13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Variable Functions<br />

45<br />

n<br />

n<br />

n<br />

n<br />

n<br />

n<br />

n<br />

n<br />

n<br />

n<br />

n<br />

is_array()—Checks whether the variable is an array.<br />

is_double(), is_float(), is_real() (All the same function)—Checks whether<br />

the variable is a float.<br />

is_long(), is_int(), is_integer() (All the same function)—Checks whether<br />

the variable is an integer.<br />

is_string()—Checks whether the variable is a string.<br />

is_bool()—Checks whether the variable is a boolean.<br />

is_object()—Checks whether the variable is an object.<br />

is_resource()—Checks whether the variable is a resource.<br />

is_null()—Checks whether the variable is null.<br />

is_scalar()—Checks whether the variable is a scalar, that is, an integer, boolean,<br />

string, or float.<br />

is_numeric()—Checks whether the variable is any kind of number or a numeric<br />

string.<br />

is_callable()—Checks whether the variable is the name of a valid function.<br />

Testing Variable Status<br />

<strong>PHP</strong> has several functions for testing the status of a variable.The first is isset(), which<br />

has the following prototype:<br />

bool isset(mixed var);[;mixed var[,...]])<br />

This function takes a variable name as an argument <strong>and</strong> returns true if it exists <strong>and</strong><br />

false otherwise.You can also pass in a comma-separated list of variables, <strong>and</strong> isset()<br />

will return true if all the variables are set.<br />

You can wipe a variable out of existence by using its companion function, unset(),<br />

which has the following prototype:<br />

void unset(mixed var);[;mixed var[,...]])<br />

This function gets rid of the variable it is passed.<br />

The empty() function checks to see whether a variable exists <strong>and</strong> has a nonempty,<br />

nonzero value; it returns true or false accordingly. It has the following prototype:<br />

bool empty(mixed var);<br />

Let’s look at an example using these three functions.<br />

Try adding the following code to your script temporarily:<br />

echo ‘isset($tireqty): ‘.isset($tireqty).’’;<br />

echo ‘isset($nothere): ‘.isset($nothere).’’;<br />

echo ‘empty($tireqty): ‘.empty($tireqty).’’;<br />

echo ‘empty($nothere): ‘.empty($nothere).’’;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!