Specifications

Testing Variable Status
PHP has several functions for testing the status of a variable.
The first of these is isset(), which has the following prototype:
int isset(mixed var);
This function takes a variable name as argument and returns true if it exists and false other-
wise.
You can wipe a variable out of existence by using its companion function, unset(). This has
the following prototype:
int unset(mixed var);
This gets rid of the variable it is passed and returns true.
Finally there is empty(). This checks to see if a variable exists and has a non-empty, non-zero
value and returns true or false accordingly. It has the following prototype:
int empty(mixed var);
Lets look at an example using these three functions.
Try adding the following code to your script temporarily:
echo isset($tireqty);
echo isset($nothere);
echo empty($tireqty);
echo empty($nothere);
Refresh the page to see the results.
The variable
$tireqty should return true from isset() regardless of what value you entered or
didnt enter in that form field. Whether it is
empty() or not depends on what you entered in it.
The variable $nothere does not exist, so it will generate a false result from isset() and a
true result from empty().
These functions can be handy in making sure that the user filled out the appropriate fields in
the form.
Reinterpreting Variables
You can achieve the equivalent of casting a variable by calling a function. The three functions
that can be useful for this are
int intval(mixed var);
double doubleval(mixed var);
string strval(mixed var);
PHP Crash Course
C
HAPTER 1
1
PHP CRASH
COURSE
37
03 7842 CH01 3/6/01 3:39 PM Page 37