diff --git a/language/types.xml b/language/types.xml index 703c1b44dc..271dfba985 100644 --- a/language/types.xml +++ b/language/types.xml @@ -1,5 +1,5 @@ - + Types @@ -667,16 +667,25 @@ EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM}) ]]> @@ -847,7 +856,8 @@ EOT; Variable parsing When a string is specified in double quotes or with - heredoc, variables are parsed within it. + heredoc, variables are + parsed within it. There are two types of syntax, a @@ -856,7 +866,8 @@ EOT; complex one. The simple syntax is the most common and convenient, it provides a way - to parse a variable, an array value, or an object property. + to parse a variable, an array value, or an + object property. The complex syntax was introduced in PHP 4, and can be recognised @@ -879,14 +890,15 @@ $beer = 'Heineken'; echo "$beer's taste is great"; // works, "'" is an invalid character for varnames echo "He drank some $beers"; // won't work, 's' is a valid character for varnames echo "He drank some ${beer}s"; // works +echo "He drank some {$beer}s"; // works ?> ]]> - Similarly, you can also have an array index or an object - property parsed. With array indices, the closing square bracket - (]) marks the end of the index. For + Similarly, you can also have an array index or an + object property parsed. With array indices, the closing square + bracket (]) marks the end of the index. For object properties the same rules apply as to simple variables, though with object properties there doesn't exist a trick like the one with variables. @@ -901,11 +913,32 @@ echo "He drank some ${beer}s"; // works 'red', 'banana' => 'yellow'); -// note that this works differently outside string-quotes +// Works but note that this works differently outside string-quotes echo "A banana is $fruits[banana]."; +// Works +echo "A banana is {$fruits['banana']}."; + +// Works but PHP looks for a constant named banana first +// as described below. +echo "A banana is {$fruits[banana]}."; + +// Won't work, use braces. This results in a parse error. +echo "A banana is $fruits['banana']."; + +// Works +echo "A banana is " . $fruits['banana'] . "."; + +// Works echo "This square is $square->width meters broad."; // Won't work. For a solution, see the complex syntax. @@ -944,18 +977,39 @@ echo "This square is $square->{width}00 centimeters broad."; width}00 centimeters broad."; + +// Works echo "This works: {$arr[4][3]}"; -// This is wrong for the same reason -// as $foo[bar] is wrong outside a string. +// This is wrong for the same reason as $foo[bar] is wrong +// outside a string. In otherwords, it will still work but +// because PHP first looks for a constant named foo, it will +// throw an error of level E_NOTICE (undefined constant). echo "This is wrong: {$arr[foo][3]}"; -echo "You should do it this way: {$arr['foo'][3]}"; +// Works. When using multi-dimensional arrays, always use +// braces around arrays when inside of strings +echo "This works: {$arr['foo'][3]}"; + +// Works. +echo "This works: " . $arr['foo'][3]; + echo "You can even write {$obj->values[3]->name}"; + echo "This is the value of the var named $name: {${$name}}"; ?> ]]> @@ -994,6 +1048,9 @@ echo "I'd like to have another {${ strrev('reeb') }}, hips"; $str = 'This is a test.'; $first = $str{0}; +// Get the third character of a string +$third = $str{2}; + // Get the last character of a string. $str = 'This is still a test.'; $last = $str{strlen($str)-1}; @@ -1045,36 +1102,41 @@ $last = $str{strlen($str)-1}; is automatically done in the scope of an expression for you where a string is needed. This happens when you use the echo or print functions, or when you compare a variable - value to a string. + value to a string. Reading the manual sections on Types and Type Juggling will make + the following clearer. See also settype. - A boolean &true; value is converted to the string "1", + A boolean &true; value is converted to the string "1", the &false; value is represented as "" (empty string). This way you can convert back and forth between boolean and string values. - An integer or a floating point number is converted to a string - representing the number with its digits (includig the exponent part - for floating point numbers). + An integer or a floating point number (float) + is converted to a string representing the number with its digits + (including the exponent part for floating point numbers). Arrays are always converted to the string "Array", - so you cannot dump out the contents of an array with echo - or print to see what is inside them. See the information - below for more tips. + so you cannot dump out the contents of an array with + echo or print to see what is inside + them. To view one element, you'd do something like + echo $arr['foo']. See below for tips on dumping/viewing the + entire contents. Objects are always converted to the string "Object". - If you would like to print out the member variable values of an object - for debugging reasons, read the paragraphs below. If you would - like to find out the class name of which an object is an instance of, - use get_class. + If you would like to print out the member variable values of an + object for debugging reasons, read the paragraphs + below. If you would like to find out the class name of which an object + is an instance of, use get_class. Resources are always converted to strings with the structure "Resource id #1" where 1 is - the unique number of the resource assigned by PHP during runtime. + the unique number of the resource assigned by PHP during runtime. If you would like to get the type of the resource, use get_resource_type. @@ -1123,14 +1185,14 @@ $last = $str{strlen($str)-1}; ]]> @@ -1212,7 +1274,10 @@ array( key => "bar", 12 => true); +$arr = array("foo" => "bar", 12 => true); + +echo $arr["foo"]; // bar +echo $arr[12]; // 1 ?> ]]> @@ -1234,7 +1299,11 @@ array("foo" => "bar", 12 => true); array(6 => 5, 13 => 9, "a" => 43)); +$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42)); + +echo $arr["somearray"][6]; // 5 +echo $arr["somearray"][13]; // 9 +echo $arr["somearray"]["a"]; // 42 ?> ]]> @@ -1244,7 +1313,7 @@ array("somearray" => array(6 => 5, 13 => 9, "a" => 43)); If you omit a key, the maximum of the integer-indices is taken, and the new key will be that maximum + 1. As integers can be negative, this is also true for negative indices. Having e.g. the highest index - being -6 will result in being -5 + being -6 will result in -5 being the new key. If no integer-indices exist yet, the key will be 0 (zero). If you specify a key that already has a value assigned to it, that value will be overwritten. @@ -1390,6 +1459,64 @@ echo $foo[bar]; undefined constant gets converted to a string of the same name automatically for backward compatibility reasons. + + More examples to demonstrate this fact: + + + 'apple', 'veggie' => 'carrot'); + +// Correct +print $arr['fruit']; // apple +print $arr['veggie']; // carrot + +// Incorrect. This works but also throws a PHP error of +// level E_NOTICE because of an undefined constant named fruit +// +// Notice: Use of undefined constant fruit - assumed 'fruit' in... +print $arr[fruit]; // apple + +// Let's define a constant to demonstrate what's going on. We +// will assign value 'veggie' to a constant named fruit. +define('fruit','veggie'); + +// Notice the difference now +print $arr['fruit']; // apple +print $arr[fruit]; // carrot + +// The following is okay as it's inside a string. Constants are not +// looked for within strings so no E_NOTICE error here +print "Hello $arr[fruit]"; // Hello apple + +// With one exception, braces surrounding arrays within strings +// allows constants to be looked for +print "Hello {$arr[fruit]}"; // Hello carrot +print "Hello {$arr['fruit']}"; // Hello apple + +// This will not work, results in a parse error such as: +// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING' +// This of course applies to using autoglobals in strings as well +print "Hello $arr['fruit']"; +print "Hello $_GET['foo']"; + +// Concatenation is another option +print "Hello " . $arr['fruit']; // Hello apple +?> +]]> + + + + + When you turn error_reporting up to show + E_NOTICE level errors (such as setting + it to E_ALL) then you will see these + errors. By default, + error_reporting is turned down to not show them. + As stated in the syntax section, there must be an expression between the @@ -1399,7 +1526,7 @@ echo $foo[bar]; ]]> @@ -1437,7 +1564,8 @@ $error_descriptions[8] = "This is just an informal notice"; because E_ERROR equals 1, etc. - Then, how is it possible that $foo[bar] works? + As we already explained in the above examples, + $foo[bar] still works but is wrong. It works, because bar is due to its syntax expected to be a constant expression. However, in this case no constant with the name bar exists. PHP now @@ -1455,48 +1583,14 @@ $error_descriptions[8] = "This is just an informal notice"; default this way, since they are special reserved keywords. - - - When you turn error_reporting to E_ALL, - you will see that PHP generates notices whenever an - index is used which is not defined. - Consider this script: - - - "y"); - -// Access element with the *bad* method -echo $abc[x]; - -?> -]]> - - - The output is: - - - -Notice: Use of undefined constant x - assumed 'x' in /path/to/script.php on -line 10
-]]> -
-
-
-
- Inside a double-quoted string, another syntax - is valid. See variable parsing in strings for more details. + To reiterate, inside a double-quoted string, it's + valid to not surround array indexes with quotes so + "$foo[bar]" is valid. See the above + examples for details on why as well as the section on + variable parsing + in strings. @@ -1507,15 +1601,17 @@ line 10
Converting to array - For any of the types: integer, float, string, boolean and resource, - if you convert a value to an array, you get an array with one element - (with index 0), which is the scalar value you started with. + For any of the types: integer, float, + string, boolean and resource, + if you convert a value to an array, you get an array + with one element (with index 0), which is the scalar value you + started with. - If you convert an object to an array, you get the properties (member - variables) of that object as the array's elements. The keys are the - member variable names. + If you convert an object to an array, you get the + properties (member variables) of that object as the array's elements. + The keys are the member variable names.