16 – PHP Type Casting

0
553

In this tutorial, we’re going to discuss about PHP Type Casting. We can call Type casting or Type Switching or Type Juggling. The idea here is that, out of all types we’ve been working in PHP such numbers or strings, we can actually switch from one type to another.

To start this application lets create a new PHP file called “typecasting.php”. And add the following code.

The PHP code below is trying to combine the string value of 10 to 4. Then we echo the variable value and it returns 14 meaning it will able to perform a mathematical operation. But this is a bad programming because we don’t want to make this switch for us, and we want a correct type to do the computation for us.

  1. <HMTL>
  2. <title>Type Casting</title>
  3. <body>
  4.     <?php
  5.         $value1 = "10";//take note that this is a string
  6.         $value1 += 4; // and this a number
  7.         echo $value1;
  8.  
  9.     ?>
  10. </body>
  11. </HTML>

Next we’re going to modify our code using the gettype function that will tell us what type that we are working with.

  1. <HMTL>
  2. <title>Type Casting</title>
  3. <body>
  4.     <?php
  5.         $value1 = "10 is my age";//take note that this is a string
  6.         $value2 = $value1 + 4; // and this a number
  7.         echo $value2;
  8.         echo '<br/>';
  9.         echo gettype($value1);//this will return a result as string
  10.         echo '<br/>';
  11.         echo gettype($value2);//this will return a result as Integer
  12.  
  13.     ?>
  14. </body>
  15. </HTML>

Output:
14
string
integer

At this time, we’re going to switch the type for the ourselves. And we can do it using thesettype function is used to set the type of a variable. Here’s the modified code below using settype function.

  1. <HMTL>
  2. <title>Type Casting</title>
  3. <body>
  4.     <?php
  5.         $value1 = "10 is my age";//take note that this is a string
  6.         $value2 = $value1 + 4; // and this a number
  7.         echo $value2;
  8.         echo '<br/>';
  9.         echo gettype($value1);//this will return a result as string
  10.         echo '<br/>';
  11.         echo gettype($value2);//this will return a result as Integer
  12.         settype($value2, "string");
  13.         echo '<br/>';
  14.         echo gettype($value2);//this will return a result as Integer
  15.  
  16.     ?>
  17. </body>
  18. </HTML>

Output:
14
string
integer
string

The other way to set a type on something is going to be referring to it using the first type. And here’s the following code.

  1. <HMTL>
  2. <title>Type Casting</title>
  3. <body>
  4.     <?php
  5.         $value1 = "10 is my age";//take note that this is a string
  6.         $value2 = $value1 + 4; // and this a number
  7.         echo $value2;
  8.         echo '<br/>';
  9.         echo gettype($value1);//this will return a result as string
  10.         echo '<br/>';
  11.         echo gettype($value2);//this will return a result as Integer
  12.         settype($value2, "string");
  13.         echo '<br/>';
  14.         echo gettype($value2);//this will return a result as Integer
  15.         $value3 = (int) $value1;
  16.         echo '<br/>';
  17.         echo gettype($value3);//this will return a result as Integer
  18.  
  19.     ?>
  20. </body>
  21. </HTML>

Output:
14
string
integer
string
integer

Facebook Comments
(Visited 24 times, 1 visits today)
SHARE
Previous articlePHP Foreach Loop
Next article09 – PHP-Accessing File
Hello Itsourcecoders, welcome to itsourcecode.com. I'm Joken Villanueva, MIT a passionate Blogger, Programmer and a Hobbyist. I started Itsourcecode because I wanted to give back and Share all the learnings and knowledge I've learned in my career and I believe through this website I would be able to help and assist those newbie programmers in enhancing their skills from different programming languages. So let us all help each other by sharing our ideas!

LEAVE A REPLY

Please enter your comment!
Please enter your name here