Type Casting Functions PHP Essential Training

Type Casting Functions PHP Essential Training

PHP

Type Casting Functions PHP Essential Training

Welcome to another fun tutorial in PHP Essential Training. In this lesson we're teach you what is type casting and why it is used in PHP. Here in Type Casting Functions PHP Essential Training we test what format of variable used in our program and change the type of variable from one type to another like, c9hange from string to integer and from any type to any type.
Type casting is also used for check the variable which type of value it can handle. As we told earlier in our previous lessons the PHP itself assumes the variable type based on its values stored. Consider the following example to understand this scenario.
// Here we give the value 2 inside the double quotes which means a string but it still act as a integer
$var1 = "2";
// Add the value with the initial value of the $var1
$var1 += 3;
// Display the Result
echo '$var1: ' . $var1;
Type Casting Functions PHP Essential Training
If we change some values in the variable $var1 to change the value from integer to string add some extra text with that and add the value in another variable.
$var1 = "2 Candies For Me ";
// Add the value with the initial value of the $var1
$var2 = $var1 + 3;
// Display the Result> Now it should return the string value
echo '$var2: ' . $var2 . "<br />" ;
Type Casting Functions PHP Essential Training
After change the code then also it doesn't change from the initial variable type. But for some purpose we wants to change the variable type. Then only we move to casting options. In PHP it has many type casting and variable checking related variables. Lets see the each functions one by one with their operations.
The gettype() and settype() are the functions which are used to set and get what kind of data a particular variable can handles.
// Get the variable type of $var1
echo '$var1: ' . gettype($var1) . "<br />";
// Get the variable type of $var2
echo '$var2: ' . gettype($var2) . "<br />";
// Set the variable type to $var2 as String
settype($var2, "string");
// Get the variable type of $var2 after change the variable type
echo '$var2: ' . gettype($var2) . "<br />";
// Assign the variable type of $var1 to $var3
$var3 = (int) $var1;
// Get the variable type of $var3
echo '$var3: ' . gettype($var3) . "<br />";
Type Casting Functions PHP Essential Training

Type Casting Functions PHP Essential Training

The above are the methods to get and set the variable types in various conditions. In Type Casting Functions PHP Essential Training we have some other functions for testing the availability of the variable types. Lets check some of them at here. Consider the variable $var1 we know already it is an String variable and check it.
In our last lesson we check the values with true or false. Here also we use the same procedure to check the functionality of a variable. Consider the following example.
echo "Is Array: " . is_array($var1) . "<br />";
// Check is it a Boolean Variable or not
echo "Is Boolean: " . is_bool($var1) . "<br />";
// Check is it a Float Variable or not
echo "Is Float: " . is_float($var1) . "<br />";
// Check is it a Integer Variable or not
echo "Is Integer: " . is_int($var1) . "<br />";
// Check is it a Null Variable or not
echo "Is Null: " . is_null($var1) . "<br />";
// Check is it a Numeric Variable or not
echo "Is Numeric: " . is_numeric($var1) . "<br />";
// Check is it a String Variable or not
echo "Is String: " . is_string($var1) . "
";
Type Casting Functions PHP Essential Training
These are the operations and functions of the Type casting functions PHP Essential Training series. Our next lesson is the constants.
Download the source Code of Type Casting Functions PHP Essential Training from here.

Leave a Reply