Learn PHP - Lesson 2 Tutorial - What are variables in PHP - Part 3 of 5, Type casting/Type juggling variables in PHP
Bookmark and Share
 Learn PHP - Lesson 2 Tutorial - What are variables in PHP - Part 3 of 5, Type casting | Type juggling variables in PHP
Lesson 2 Overview
In this lesson, we will cover the following: 

1) What a variable is.
2) Different types of variables
3) Type casting | Type juggling variables in PHP
4) Superglobal variables in PHP - Superglobals
5) Advanced applications of a variable you will one day need to know.



Part 3 of 5, Type casting | Type juggling variables in PHP

If you are a programmer of a compiled language (java, c, etc.) then you know what this is and all you need to know is that PHP takes care of this for you on a basic level.  There may be some extremely advanced situations where you'll want to set it, but you can skip this quick section and move on.

For everyone else just learning... read on. This is going to be a quick and simple lesson.

In other programming languages, you may have to tell the interpreter what type(s) of variables you are playing with.  For instance.  If I'm running a basic math equation:

10 + 8


in other languages, you would have to tell the interpreter that 10 and 8 are numbers:

10(integer) + 8(integer)

in order for the calculation to work correctly.

Within PHP, PHP assumes a variables type, depending on what you assign to it (this is called type juggling).

For example:

$a = 'TRUE'; // PHP assumes this is a string type because it is text in quotations

$a = TRUE; // PHP assumes this is a Boolean type because it is "TRUE" or "FALSE" without the quotations

$a = 10
// PHP assumes this is an integer type because it's a whole number withot quotations

Also part of type juggling is the ability of PHP to be able to figure out what you mean.  Let's say you want to add 10 + 8 again, but you accidentally enclosed them in single quotes... EXAMPLE:


$a = '8';

$b = '10';

$c = $a + $b;


echo $c;

PHP is smart enough to make the appropriate calculations.

Now in the event you really need to type cast a variable, JUST MAKE SURE YOU SET THE VARIABLE FIRST, THEN ASSIGN IT A TYPE (TYPE CASTING). Example

$str = 88;


$string = (string) $str;



PHP TYPE CASTING CHART:

These type castings are allowed:

(int), (integer) - cast to integer
(bool), (boolean) - cast to boolean
(float), (double), (real) - cast to float
(string) - cast to string
(array) - cast to array
(object) - cast to object
(unset) - cast to NULL (PHP 5)
(binary) casting and b prefix forward support was added in PHP 5.2.1

Update index.php on your web server and play around with it... here's a file to start with of our examples

<?php

$a = '8';

$b = '10';

$c = $a + $b;

echo $c . '<br /><br />';


$str = 88;

$string = (string) $str;

echo 'type of variable is: '.gettype($string);
// gettype() will return the type of variable in a string format


?>






Go to Part 4 »
« Back to Part 2
« Back to Lesson 1
« Back to Table of Contents
Recent Comments