note 99873 added to language.variables.basics

From: Date: Sat, 11 Sep 2010 23:11:10 +0000
Subject: note 99873 added to language.variables.basics
Groups: php.notes 
Request: Send a blank email to php-notes+get-172488@lists.php.net to get a copy of this message
This page should include a note on variable lifecycle: Before a variable is used, it has no existence. It is unset. It is possible to check if a variable doesn't exist by using isset(). This returns true provided the variable exists and isn't set to null. With the exception of null, the value a variable holds plays no part in determining whether a variable is set. Setting an existing variable to null is a way of unsetting a variable. Another way is variables may be destroyed by using the unset() construct. <?php print isset($a); // $a is not set. Prints false. (Or more accurately prints ''.) $b = 0; // isset($b) returns true (or more accurately '1') $c = array(); // isset($c) returns true $b = null; // Now isset($b) returns false; unset($c); // Now isset($c) returns false; ?> is_null() is an equivalent test to checking that isset() is false. The first time that a variable is used in a scope, it's automatically created. After this isset is true. At the point at which it is created it also receives a type according to the context. <?php $a_bool = true; // a boolean $a_str = 'foo'; // a string ?> If it is used without having been given a value then it is uninitalized and it receives the default value for the type. The default values are the _empty_ values. E.g Booleans default to FALSE, integers and floats default to zero, strings to the empty string '', arrays to the empty array. A variable can be tested for emptiness using empty(); <?php $a = 0; //This isset, but is empty ?> Unset variables are also empty. <?php empty($vessel); // returns true. Also $vessel is unset. ?> Everything above applies to array elements too. <?php $item = array(); //Now isset($item) returns true. But isset($item['unicorn']) is false. //empty($item) is true, and so is empty($item['unicorn'] $item['unicorn'] = ''; //Now isset($item['unicorn']) is true. And empty($item) is false. //But empty($item['unicorn']) is still true; $item['unicorn'] = 'Pink unicorn'; //isset($item['unicorn']) is still true. And empty($item) is still false. //But now empty($item['unicorn']) is false; ?> For arrays, this is important because accessing a non-existent array item can trigger errors; you may want to test arrays and array items for existence with isset before using them. ---- Server IP: 69.147.83.197 Probable Submitter: 86.9.150.190 ---- Manual Page -- http://www.php.net/manual/en/language.variables.basics.php Edit -- https://master.php.net/note/edit/99873 Del: integrated -- https://master.php.net/note/delete/99873/integrated Del: useless -- https://master.php.net/note/delete/99873/useless Del: bad code -- https://master.php.net/note/delete/99873/bad+code Del: spam -- https://master.php.net/note/delete/99873/spam Del: non-english -- https://master.php.net/note/delete/99873/non-english Del: in docs -- https://master.php.net/note/delete/99873/in+docs Del: other reasons-- https://master.php.net/note/delete/99873 Reject -- https://master.php.net/note/reject/99873 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172488) next »