PHP Variables Introduction - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Variables Introduction - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript


Post Top Ad

Post Top Ad

Tuesday, May 21, 2019

PHP Variables Introduction



Along with conditional logic, variables are the core of what makes computer programs
powerful and flexible. If you think of a variable as a bucket with a name that holds a value, PHP lets you have plain old buckets, buckets that contain the name of other buckets, buckets with numbers or strings in them, buckets holding arrays of other buckets, buckets full of objects, and just about any other variation on that analogy you can think of.

A variable is either set or unset. A variable with any value assigned to it, true or false, empty or nonempty, is set. The function isset() returns true when passed a variable that’s set. To turn a variable that’s set into one that’s unset, call unset() on the variable or assign null to the variable. Scalars, arrays, and objects can all be passed to unset(). You can also pass unset() multiple variables to unset them all:

           unset($earth, $moon, $stars);

If a variable is present in the query string of a URL, even if it has no value assigned to it, it is set in the appropriate superglobal array. Thus:


sets $_GET['monkeys'] to 12 and $_GET['chimps'] to the empty string.

All unset variables are also empty. Set variables may be empty or nonempty. Empty variables have values that evaluate to false as a boolean. These are listed in Table.

Table   Values that evaluate to false

Type   Value                                                 
integer    0
double    0.0
string      “” (empty string)
string      “0”
boolean  false
array       array() (empty array)
null         NULL
object     An object with no properties, only prior to PHP 5

Everything else not listed in Table is nonempty. This includes the string "00", and the string " ", containing just a space character.

Variables evaluate to either true or false. The values in Table are the complete set of what’s false in PHP. Every other value is true. The language construct isset() tells you whether a variable is set. The language construct empty() tells you whether a value is empty or not. In versions of PHP prior to 5.5 empty() only accepts variables as arguments. In PHP 5.5, you can pass an arbitrary expression to empty().

Constants and return values from functions can be false, but before PHP 5.5, they can’t be empty. For example, Example shows a valid use of empty() (in any PHP version) because $first_name is a variable.

Example   Correctly checking if a variable is empty

           if (empty($first_name)) { .. }

On the other hand, the code in Example  returns parse errors before PHP 5.5 because 0 (a constant) and the return value from get_first_name() can’t be empty.

Example   Incorrectly checking if a constant is empty before PHP 5.5

           if (empty(0)) { .. }
           if (empty(get_first_name())) { .. }

No comments:

Post a Comment

Post Top Ad