Reworded how typing works and rewrote example to help show it.

Fixed bug #45207 (Incorrect or missing documentation for languages.variables)


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@267083 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Ross Masters 2008-10-08 19:50:07 +00:00
parent 056b700f20
commit 07e081dd58

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.103 $ -->
<!-- $Revision: 1.104 $ -->
<chapter xml:id="language.variables" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Variables</title>
@ -121,8 +121,9 @@ $bar = &test(); // Invalid.
<para>
It is not necessary to initialize variables in PHP however it is a very
good practice. Uninitialized variables have a default value of their type
- &false;, zero, empty string or an empty array.
good practice. Uninitialized variables have a default value of their type depending on the context in which they are used
- booleans default to &false;, integers and floats default to zero, strings (e.g. used in <function>echo</function>) are
set as an empty string and arrays become to an empty array.
</para>
<para>
<example>
@ -130,10 +131,32 @@ $bar = &test(); // Invalid.
<programlisting role="php">
<![CDATA[
<?php
echo ($unset_bool ? "true" : "false"); // false
// Unset AND unreferenced (no use context) variable; outputs NULL
var_dump($unset_var);
// Boolean usage; outputs 'false' (See ternary operators for more on this syntax)
echo($unset_bool ? "true\n" : "false\n");
// String usage; outputs 'string(3) "abc"'
$unset_str .= 'abc';
var_dump($unset_str);
// Integer usage; outputs 'int(25)'
$unset_int += 25; // 0 + 25 => 25
echo $unset_string . "abc"; // "" . "abc" => "abc"
$unset_array[3] = "def"; // array() + array(3 => "def") => array(3 => "def")
var_dump($unset_int);
// Float/double usage; outputs 'float(1.25)'
$unset_float += 1.25;
var_dump($unset_float);
// Array usage; outputs array(1) { [3]=> string(3) "def" }
$unset_arr[3] = "def"; // array() + array(3 => "def") => array(3 => "def")
var_dump($unset_arr);
// Object usage; creates new stdClass object (see http://www.php.net/manual/en/reserved.classes.php)
// Outputs: object(stdClass)#1 (1) { ["foo"]=> string(3) "bar" }
$unset_obj->foo = 'bar';
var_dump($unset_obj);
?>
]]>
</programlisting>