Showing posts with label PHP TYPE CASTING. Show all posts
Showing posts with label PHP TYPE CASTING. Show all posts

Tuesday, 18 September 2018

Type casting with PHP

PHP is a loosely typed language and assigns types to variables depending what is assigned to it. Variables coming from get/post and cookies etc are generally cast as strings rather than other types and there are often other times when you need to specifically cast a type in PHP as e.g. an integer. This post looks at how to type cast in PHP and some of the results than can come about from type casting.
There are two ways to cast a variable in PHP as a specific type: using the settype() function, or using (int) (bool) (float) etc. Using the settype function you can do this:
settype($foo, "array");
settype($foo, "bool");
settype($foo, "boolean");
settype($foo, "float");
settype($foo, "int");
settype($foo, "integer");
settype($foo, "null");
settype($foo, "object");
settype($foo, "string");
Using (int) etc you can do this instead:
$foo = (array)$foo;
$foo = (b)$foo;      // from PHP 5.2.1
$foo = (binary)$foo; // from PHP 5.2.1
$foo = (bool)$foo;
$foo = (boolean)$foo;
$foo = (double)$foo;
$foo = (float)$foo;
$foo = (int)$foo;
$foo = (integer)$foo;
$foo = (object)$foo;
$foo = (real)$foo;
$foo = (string)$foo;
An example of this in action is as follows, where $foo starts off as a string value and is then cast into an integer:
$foo = '1';
echo gettype($foo); // outputs 'string'
settype($foo, 'integer');
echo gettype($foo); // outputs 'integer'
The equivilent way of doing this with (int) would look like this instead:
$foo = '1';
echo gettype($foo); // outputs 'string' 
$foo = (int)$foo;
echo gettype($foo); // outputs 'integer'
I often find the (int) function useful when needing to output the value of a boolean value when debugging and testing. You can do this for example:
$foo = true;
echo (int)$foo; // outputs 1
$foo = false;
echo (int)$foo; // outputs 0
Without the type casting, echoing $foo when it's false won't show anything.
This post will be followed by a second next week looking at the results of type casting conversion, such as when you convert a string to an integer with PHP.

Related posts:

Thursday, 4 September 2014

How would you convert a PHP variable to a string? Is there something like the toString method that Java has in PHP?

PHP has casting operators that can be used to convert non-string variables into strings. Here is how to use a casting operator to convert a variable to a string in PHP:

Example of PHP’s equivalent to toString

// this is an integer:
$nonStringVar = 123;

/*now $stringVar is a string because
   the "(string)" performs a type cast
   and returns the string equivalent
   of the integer
*/
$stringVar = (string)$nonStringVar;
The casting operator is fairly close in functionality to the toString method in Java, but read below because PHP also has a it’s own __toString method.

Using the strval function to convert a variable to a string

You can also use the function strval to get the string value of a variable.
// this is an integer:
$nonStringVar = 123;

//now $stringVar is a string
$aString = strval($nonStringVar);

PHP does have a __toString magic method


PHP also provides a method called __toString that is defined by the programmer, and that basically tells a class how to act when it is treated like a string. When would a class be treated like a string? Well, suppose you have a class called SomeClass, and an object of that class called $someObject. If you decide to do something like this: “echo $someObject;”, then what exactly should be output in that scenario? Well, that is up to you to decide – because whatever you define in __toString method is what will be output to the page. You can decide to output all of the class’s instance variables, output a particular string, etc. – but whatever you do the __toString method will have to return a string.
Here is an example of the __toString method in action:
class SomeClass
{
    public $aVariable;

    public function __construct($aVariable)
    {
        $this->aVariable =  $aVariable;
    }

    public function __toString()
    {
        return $this->aVariable;
    }
}

$someObject = new SomeClass('Testing 123');


/* this will indirectly call the toString method:
    which will output the string 'Testing 123'
*/
echo $someObject;


The __toString method is a magic method – you can read more about magic methods