MENU
Strings
A string may may span multiple lines without using any multiline character. Strings can be joined with the . operator. Applying an arithmetic operator to a string converts the string to a number, while applying ++ and to a string variable shifts its last character to the next character. Eg.:echo (30+”10.5 meters”); //outputs 40.5
$a=”bus”; $a++; // $a is now “but”
<,<=,> and>= compare the character codes of the first characters of the strings. If they are the same, the second characters will be compared, and so on. For example: "ACD">"ABD" is true.
In double-quoted strings, the following will escape:
$variable, \n(new line),\r(carriage return), \t (horizontal tab),\v(vertical tab),\e(escape),\f(form feed),\\(backslash),\$(dollar sign),\”(double quote),\ddd(octal sequence), and \xdd(hexadecimal sequence).
In single-quoted strings, only \\ (backslash) and \’(single quote) will escape.
RESETRUNFULL
<!DOCTYPE html>
<html>
<head></head>
<body><?php
$a=5;
echo ("Give me $a" . "<br />");
echo ('Give me $a' . '<br />');
echo ('This isn\'t "bad"' . '<br />');
echo ("\n");
echo ('\n');
?></body>
</html>
If the opening identifier is not quoted or double-quoted, characters expand and escape as in double quoted strings. If the opening identifier is single-quoted, nothing expands nor escapes, not even \\ and \’.
RESETRUNFULL
<!DOCTYPE html>
<html>
<head></head>
<body><?php
$a=3;
echo <<<MSG1
Testing $a \\ " <br />
MSG1;
echo <<<'MSG2'
Testing $a \\ \'
MSG2;
?></body>
</html>
Omitting the curly brackets will cause an error because $fruits is undefined.
A single character in a string may be accessed with [] or {}:<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
$drinks=['Cola','7 Up','Sarsi'];
$fruit='apple';
echo ("He drank some $drinks[1]. ");
echo ("He ate some {$fruit}s. ");
echo ("I said ${fruit}s.");
?>
</body>
</html>
$str=”abcd”;
$str[2]=”e”; // changes the string to “abed”
echo ($str{0}); // outputs “a”
A string can be executed as if it is some PHP code with eval(…).Eg.: eval(“echo 100; return 100;”); will return 100. eval(…) returns NULL by default, and returns false if there is a parse error within.
A string may be used as the name of a variable. For instance, {$a=”hello”; $$a=”world”;} is equivalent to {$a=”hello”; $hello=world;}. In the case of an array, ${$a[1]} treats $a[1] as the variable name while ${$a}[1] treats $$a as the variable name.