Braces
Braces are used when parser is not able to parse a variable
$a[1] = 'Smith';
echo "John {$a[1]}[1974]"; // John Smith[1974]
Herodoc
Herodoc formating is similar with double quotes, but for multiple lines. It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;).
// Allows the use of quotes without escaping
$who = "John";
$output = <<<TEST
She said "This is $who's test"
on multiple rows
<span class='keyword_code'>TEST;</span>
echo nl2br($output);
/* output
She said "This is John's test"
on multiple rows
*/
Nowdoc
With Nowdoc formating no parsing is done inside.
$who = "John";
$str = <<<'TEST'
She said "This is $who's test"
on multiple rows
TEST;
echo nl2br($str);
/* Outputs:
She said "This is $who's test"
on multiple rows
*/
Length
All characters in the string are counted, regardless of their value.
echo strlen("abc"); // 3
echo strlen(""); // 0
echo strlen(" "); // 1
echo strlen("n"); // 1
echo strlen('n'); // 1
echo strlen(NULL); // 0
echo strlen(0); // 1
echo strlen(TRUE); // 1
echo strlen(FALSE); // 0
Concatation
Concatation with comma is faster then dot.
echo "a" . "b"; // Output: ab
echo "a" , "c"; // Output: ac
Using Strings as Arrays
$str = "abcdef";
echo $str[1]; // b
Conversion
String automatic conversion
echo "a" . "b"; // ab
echo 1 . 2; // 12
echo "a" + "b"; // 0
echo "a" + 1; // 0 + 1 = 1
echo "a" * 2; // 0 * 2 = 0
Precedence
Operator Precedence and Associativity
* / % <span class='keyword_code'>Highest Precedence</span>
+ - .
< <= > >=
= = = != =
&&
||
And
XOR
OR Lowest Precedence
// */%
echo 10 / 2 * 5 % 7; // 4
// 5 * 5 % 7
// 25 % 7 = 4
// + - .
echo "aa" . 1 + 2 . 'b'; // 2b
// "aa1" + 2 . 'b'
// 0 + 2 . 'b' = 2b
// * / % precedence over + - .
echo "aa" . 1 * 2 . '0'; // aa20
// + - . precedence over > <
echo 3 - 2 < 2 ; // 1
echo 3 - (2 < 2) ; // 3
Last update: 432 days ago