A string
(or “string-literal”)
is any number of characters enclosed within a pair of either
single (') or double (") quotation marks.
echo "This is a php string.\n";
echo 'So is this, though we tend to prefer double-quotes (read more below).\n';
echo "\n";
echo "Remember that spaces are just regular ol' characters (not 'nothing'), ";
echo "and that the empty-string is a string (not 'nothing').\n";
echo "The length of five spaces: strlen(\" \") = ", strlen(" ");
echo "The length of the empty string: strlen(\"\") = ", strlen("");
echo "The length of a string consisting of just the newline character: strlen(\"\\n\") = ", strlen("\n");
Note that unlike many languages, string-literals can contain contain newlines:
<?php
echo "hello
there
everybody!";
echo '
-- Dr. Nick
';
?>
Rule of thumb: Use double-quoted strings most of the time, in your program;
if printing/creating strings of html, let it be html whose attribute-values
contain single-quoted strings (since html lets you use either
double- or single-quotes, around attribute-values).
If using double-quotes, the and you want to include a double-quote in
your string-literal, precede that character with a backslash \".
(This case is rare, if you are following the preceding rule-of-thumb!)
You can also embed other special characters, like newlines, by using \n,
so "hi\nbye" is a six-character string, not seven.
This is probably what you expect, from most other programming languages.
Just to keep things odd: double-quoted literals allow most standard quoted characters
except for \' so "hi\'bye" is a seven-character string,
not six!
If using single-quotes, the and you want to include a single-quote in
your string-literal, precede that character with a backslash \',
sensibly enough.
However,
note that no other characters can be quoted, in single-quoted strings:
'hi\nbye' and 'hi\"bye' are both seven-character strings,
not six!
(And, variable-substitution, mentioned below, doesn't happen with single-quotes strings.)
So: No matter what type of string you're using,
don't try to quote the other type of quote-marks.
Furthermore:
If a string-literal is delimited with double-quotes contains variables (a dollar-sign),
then the variable gets replaced with its value!
This “interpolation”1 occurs
(only) within double-quoted strings, not single-quoted.
This is feature of php, to make string-generation easy.
Note:
I suggest using double-quotes for most php strings, by default.
For HTML, we'll tend to use single-quotes
String examples:
$first_name = "John";
$date = "1/01/2010"; // This is a string of course, not a "date type", nor division.
$str = "How are you?";
$str = "How are you, " . $first_name . "?";
$str = "How are you, $first_name?"; // alternately
$not = 'How are you, $first_name?'; // not what was desired, presumably.
$str2 = "I said 'how are you?'";
$str2 = "I said 'how are you $first_name, you big dope?' which was fine."; // Why does it work?
// To use a variable followed by literal-chars that aren't part of the variable name:
$str3 = "Curly braces are as cute as you are, ${first_name}y-poo!";
here-doc strings — handy for strings of many lines
which you want to copy/paste to/from elsewhere:
$sampleProg = <<<MY-PROG
Imagine having
lots and lots of code
here (including "double-quoted" strings,
and
'single-quoted'.
THis version DOES process \n as newlines
and $dollarSigns as starts of a variable-name-whose-value-gets-spliced-in.
MY-PROG;
Actually, they come in two varieties: double-quoted, and single-quoted:
double-quoted,
and
single-quoted.
PHP's echo-mode vs. PHP-mode
PHP files themselves, outside of “<?php …
?>”,
are implicit echo statements!
For example,
the following two programs produce exactly the same output.
<?php
echo "hello world\n";
echo "bye now.\n";
?>
hello world
bye now.
That's right, the text on the right is a complete php program,
and when you use php to run the program,
it is using this new rule that everything is an implicit-print-statement,
if it's not inside “<php” and “?>”.
A better example, where most of the program is implicit-prints,
but a few places do some computation, would be:
Both of these two programs, when run as php programs, will print out the same characters.
(The fact that what it prints happens to be html is irrelevent, from php's perspective.
If you happen to want to write many programs which print, and large large swaths of
what you want to print are constant but there are occasional places where you want
to
tailor the output, then yes,
it is extremely convenient to have a language where most everything is
implicit-printing,
and a short prefix brings you to a "computing" mode.
Printing web-pages is exactly such a use-case.
I'll posit:
the reason PHP is so popular, despite so many egregious design flaws,
is (a) implicit print-statments, and (b) the ease of embedding
variables inside strings with $.
even better:
Unsurprisingly, PHP also gets details wrong with its two hit features2.
Others have taken these ideas and implemented them properly:
pollen
is an authoring system (slightly web biased)
based on top of
racket's
“scribble” syntax
(a fully general-purpose setting for programs whose body is mostly fixed text/strings).
Many people are under the mistaken impression that “php programs are embedded
inside html”.
That is misguided: there is nothing special about html, and technically the whole
thing
is a program where most lines are implicit-prints.
There is no "embedding", even if it looks like it.
2
In PHP, cannot nest implicit-print mode;
it prints and doesn't return strings;
variable-inclusion takes more work for array-lookup and general expressions.
↩