- Before You Begin: Accessing PHP
- Creating a Sample Application: Bob's Auto Parts
- Embedding PHP in HTML
- Adding Dynamic Content
- Accessing Form Variables
- Understanding Identifiers
- Examining Variable Types
- Declaring and Using Constants
- Understanding Variable Scope
- Using Operators
- Working Out the Form Totals
- Understanding Precedence and Associativity
- Using Variable Handling Functions
- Making Decisions with Conditionals
- Repeating Actions Through Iteration
- Breaking Out of a Control Structure or Script
- Employing Alternative Control Structure Syntax
- Using declare
Using Variable Handling Functions
Before we leave the world of variables and operators, let’s look at PHP’s variable handling functions. PHP provides a library of functions that enable you to manipulate and test variables in different ways.
Testing and Setting Variable Types
Most of the variable functions are related to testing the type of function. The two most general are gettype() and settype(). They have the following function prototypes; that is, this is what arguments expect and what they return:
string gettype(mixed var);
bool settype(mixed var, string type);
To use gettype(), you pass it a variable. It determines the type and returns a string containing the type name: bool, int, double (for floats, confusingly, for historical reasons), string, array, object, resource, or NULL. It returns unknown type if it is not one of the standard types.
To use settype(), you pass it a variable for which you want to change the type and a string containing the new type for that variable from the previous list.
You can use these functions as follows:
$a = 56;
echo gettype($a).'<br />';
echo gettype($a).'<br />';
When gettype() is called the first time, the type of $a is integer. After the call to settype(), the type is changed to float, which is reported as double. (Be aware of this difference.)
PHP also provides some specific type-testing functions. Each takes a variable as an argument and returns either true or false. The functions are
is_array()—Checks whether the variable is an array
is_double(), is_float(), is_real() (All the same function)—Checks whether the variable is a float
is_long(), is_int(), is_integer() (All the same function)—Checks whether the variable is an integer
is_string()—Checks whether the variable is a string
is_bool()—Checks whether the variable is a boolean
is_object()—Checks whether the variable is an object
is_resource()—Checks whether the variable is a resource
is_null()—Checks whether the variable is null
is_scalar()—Checks whether the variable is a scalar—that is, an integer, boolean, string, or float
is_numeric()—Checks whether the variable is any kind of number or a numeric string
is_callable()—Checks whether the variable is the name of a valid function
Testing Variable Status
PHP has several functions for testing the status of a variable. The first is isset(), which has the following prototype:
bool isset(mixed var[, mixed var[,...]])
This function takes a variable name as an argument and returns true if it exists and false otherwise. You can also pass in a comma-separated list of variables, and isset() will return true if all the variables are set.
You can wipe a variable out of existence by using its companion function, unset(), which has the following prototype:
void unset(mixed var[, mixed var[,...]])
This function gets rid of the variable it is passed.
The empty() function checks to see whether a variable exists and has a nonempty, nonzero value; it returns true or false accordingly. It has the following prototype:
bool empty(mixed var)
Let’s look at an example using these three functions.
Try adding the following code to your script temporarily:
echo 'isset($tireqty): '.isset($tireqty).'<br />';
echo 'isset($nothere): '.isset($nothere).'<br />';
echo 'empty($tireqty): '.empty($tireqty).'<br />';
echo 'empty($nothere): '.empty($nothere).'<br />';
Refresh the page to see the results.
The variable $tireqty should return 1 (true) from isset() regardless of what value you entered in that form field and regardless of whether you entered a value at all. Whether it is empty() depends on what you entered in it.
The variable $nothere does not exist, so it generates a blank (false) result from isset() and a 1 (true) result from empty().
These functions are handy when you need to make sure that the user filled out the appropriate fields in the form.
You can achieve the equivalent of casting a variable by calling a function. The following three functions can be useful for this task:
int intval(mixed var[, int base=10])
float floatval(mixed var)
string strval(mixed var)
Each accepts a variable as input and returns the variable’s value converted to the appropriate type. The intval() function also allows you to specify the base for conversion when the variable to be converted is a string. (This way, you can convert, for example, hexadecimal strings to integers.)