Default values of uninitialized variables

suggest change

Although not necessary in PHP however it is a very good practice to initialize variables. Uninitialized variables have a default value of their type depending on the context in which they are used:

Unset AND unreferenced

var_dump($unset_var); // outputs NULL

Boolean

echo($unset_bool ? "true\n" : "false\n"); // outputs 'false'

String

$unset_str .= 'abc';
var_dump($unset_str); // outputs 'string(3) "abc"'

Integer

$unset_int += 25; // 0 + 25 => 25
var_dump($unset_int); // outputs 'int(25)'

Float/double

$unset_float += 1.25;
var_dump($unset_float); // outputs 'float(1.25)'

Array

$unset_arr[3] = "def";
var_dump($unset_arr); //  outputs array(1) {  [3]=>  string(3) "def" }

Object

$unset_obj->foo = 'bar';
var_dump($unset_obj); // Outputs: object(stdClass)#1 (1) {  ["foo"]=>  string(3) "bar" }

Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Variables:
* Default values of uninitialized variables

Table Of Contents
1 Variables
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR