Studyon Minte9.com
ZCE 5.3

Study

String Basics



	// quotes: '$an' outputs $an ; "$an" outputs "a "
	// braces: "{a[1][1974]}"
	// herodoc: <<<TEST ... TEST; (2 newline chars, no class initializing)
	// nowdoc: <<<'TEST' ... TEST; (similar with herodoc, but no parsing)
	// length: strlen(FALSE) is 0
	// concat: $a.$b OR $a,$b (faster)
	// strlen binary safe (all chars are counted) / strlen(NULL) = 0 /strlen(0) = 1
	// automatic conversion (1 . 2 = 12) ('a' + 'b' = 0) ("123abc" == 123; // true)
	// precedence */% ; +-. ; < <= > >=

	// echo 5 + "10 things"; // 15
	// echo print(2); // 21
	// echo '1'.print(2); // 211
	// echo '1'.print(2) + 3; // 511

        // $a = 'foo'; echo $a['bar']; // Output: f  // 'bar' evaluates to 0

Single quotes - will be recorded as is

<?php $a = 'a'; echo $b = '$an'; // output $an // --- Look Here --- //
  Double quotes - will be interpreted and replaced
<?php $a = 'a'; echo $b = "$an"; // output a // --- Look Here --- //
  Braces - used when parser is not able to parse a variable
<?php $a[1] = 'Smith'; echo "John {$a[1]}[1974]"; // John Smith[1974] // --- Look Here --- //
  Herodoc - 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 (;).
<?php // allows the use of quotes without escaping $who = "John"; $output = <<<TEST She said "This is $who's test" on multiple rows TEST; // --- Look Here --- // echo nl2br($output); /* output She said "This is John's test" on multiple rows */
  Nowdoc - no parsing is done inside
<?php $who = "John"; $str = <<<'TEST' // --- Look Here --- // 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 - strlen()
<?php //all characters in the string are counted, regardless of their value echo strlen("abc"); // 3 echo strlen(""); // 0 // --- Look Here --- // 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 // --- Look Here --- //
  Concatation - with comma is faster then dot
<?php echo "a" . "b"; // Output: ab echo "a" , "c"; // Output: ac // --- Look Here --- //
  Using Strings as Arrays
<?php $str = "abcdef"; echo $str[1]; // b // --- Look Here --- //
  String automatic conversion
<?php echo "a" . "b"; // Output: ab echo 1 . 2; // Output: 12 echo "a" + "b"; // Output: 0 echo "a" + 1; // 0 + 1 // --- Look Here --- // // Output 1 echo "a" * 2; // 0 * 2 // Output 0
  Operator Precedence and Associativity * / % Highest Precedence + - . < <= > >= = = = != = && || And XOR OR Lowest Precedence
<?php // * / % echo 10 / 2 * 5 % 7; // 5 * 5 % 7 = 25 % 7 = 4 // Output: 4 // + - . echo "aa" . 1 + 2 . 'b'; // Output: 2b // --- Look Here --- // // "aa1" + 2 . 'b' // 0 + 2 . 'b' = 2b // * / % precedence over + - . // --- Look Here --- // echo "aa" . 1 * 2 . '0'; // Output: aa20 // + - . precedence over > < echo 3 - 2 < 2 ; // Output 1 echo 3 - (2 < 2) ; // Output 3