PHP’s Data Types–Type Casting

You can pretend that a variable or value is of a different type by using a type cast.This
feature works identically to the way it works in C.You simply put the temporary type in
parentheses in front of the variable you want to cast.
For example, you could have declared the two variables from the preceding section
using a cast:
$totalqty = 0;
$totalamount = (float)$totalqty;

The second line means “Take the value stored in $totalqty, interpret it as a float, and
store it in $totalamount.”The $totalamount variable will be of type float.The cast
variable does not change types, so $totalqty remains of type integer.
You can also use the built-in function to test and set type, which you will learn about
later in this chapter.

Advertisements

One thought on “PHP’s Data Types–Type Casting”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s