The PHP is often referred to as "loosely typed" or "dynaically typed". What this means is that variable types are detirmined by context. Lets jump straight into some code to see it in action.
The above code will produce the following output:
string(1) "6"
int(7)
int(7)
We began with a string value of 6 and then incremented the variable using the ++ operand. PHP has noted the context of the operation and internally converted the type to INT.
Types
PHP supports many different types, these include
- int
- string
- binary
- unicode
- array
- boolean
- bool (same as boolean)
- object
- float
- double (same as float)
- real (same as float)
CASTING
The ability of PHP to be able to dynamically type variables makes for quick and lean code. But for those times where we absolutely need the variable to be of a particular type, the variable must be cast. This is done in much the same way as in C.
Cast To String
The above script shows us the variable begins as an integer and then is cast to a string.
int(6)
string(1) "6"
string(1) "6"
Cast To Int
Just as PHP allows casting to string, the same applies when casting to an integer
The above script tells that the string is created and then converted to an int.
string(2) "26"
int(26)
int(26)
Try changing the value of the value of the sting to a non-numerical value such as "this is a string". The result, after casting, would then be int(0).
Cast To Binary
Cast To Unicode
Cast To Boolean
The above script will output the following:
int(1)
bool(true)
bool(true)
The script ouputs bool(true) because "1" is considered to be a true boolean value. Other values that evaluate to boolean true are:
- -1
- "string"
- 1.6e5
So, basically, any non zero value will evalute to boolean true. Values that evaluate to boolean false are:
- 0
- "0"
- ""
- FALSE
- array()
- NULL
Cast To Object
This is quite groovy. Casting an array to an object allows the use of the array as an object, using the corresponding keys and values.
If any other type, such as a sting or integer is cast to an object, PHP creates an instance of stdClass and the value is contained within the class member scalar as shown here:
The above string outputs "this is a string" as the string value has been assigned internally by PHP to the special class member "scalar".
Cast To Float
Floats are also known as double or real values and casting is generally considered to be a bad idea with PHP as the precision needed with floats is not readily available as PHP has a finite amount of numbers. Better results can be had with the bc functions.
Check for Type
Up to this point we have dealt with converting or casting types. But there will be times when we need to check what type of variable a script is using. PHP comes with several ways to achieve this with the is_* functions and the charater type, or ctype_* functions.
Check String
In PHP most variables are strings to begin with, so this is rather straight forward.
Check INT
PHP has several ways of checking if the value of a variable is an integer, using the is_int() function is the preferred method. The is_interger() and is_long functions are both an alias of is_int().
Of course, we could use the ctype_digit() function to check also.
Note:
While the function is_numeric() also checks the value of a string for numbers, it should not be used as it also returns try for floats and scientific notification.
Check Array
To check if a value is or type array, simply use is_array() as we have seen earlier with other types, you should have the swing of this by now..
The above code outputs "bruce". As we saw in the Cast To Object section we can cast the array to an object. In this instance we have first checked that the value is indeed an array with the is_array() function.
Check Object
No surprises here as to what function is used to check if a variable is an object.. is_object().
Check Float
To check if a variable is a float or double or real type, the is_float() function is used. It should be noted that all values from super globals such as $_GET or $_POST are strings and to check the values of these the is_numeric() function should be used. Here we show how to check for a floating point number.
The above script checks the value of pi() and finds it has a value of 3.14159265359 and so, echoes the value.
Check Double
To check if a number is a double the is_double() function can be used. This function is an alias of is_float() and so the same procedure applies as seen in the section Check Float.
Check Real
To check if a number is a real type the is_real() function can be used. This function is an alias of is_float() and so the same procedure applies as seen in the section Check Float.
Check Boolean
Checking a boolean value is quite simple, just as we have seen through-out this document the is_bool() function is used.
In the above code we have checked if the number zero is an integer with the is_int() function. This returns boolean true on success so when checked with the is_bool() function we see that it is boolean and dumping the resulting variable shows bool(true).
Check Buffer
This bad boy checks if a variable is a native unicode or binary string. The is_buffer() function works the same as the other is_* functions
Check Binary
To check if variable is a native binary, use the is_binary() function.
Check Unicode
To check if a string is a unicode string, the is_unicode() function is used as below.
Check NULL
To find if a variable value is NULL the is_null() function is used.
Check Resource
A resource is a type returned by PHP from opened files and database connections. Using the is_resource() function a variable can be checked reliably.
Check Scalar
A scalar value is a variable value of type boolean, string, integer or float. All other types are considered non-scalar values. To check for a scalar value use the is_scalar() function.
<?php
/*** a string ***/$string ="5.2";
/*** check if value is scalar ***/if (is_scalar($string))
{
echo $string.' is scalar';
}
?>
0 comments:
Post a Comment