Type conversion in PHP

Getting the type of a variable

To get the type of a variable, use the gettype() function, which returns the name of the type of the variable, such as integer(integer), double(float), string(string), boolean(boolean), null, array(array), object(object), or unknown type. For example:

 
<?php
$a = 10;
$b = "10";
echo gettype($a); // integer
echo "<br>";
echo gettype($b); // string
?>

There are also a number of special functions that return true or false depending on whether the variable represents a particular type:

  • is_integer($a) : returns true if $a is an integer
  • is_string($a) : returns true if $a is a string
  • is_double($a) : returns true if $a is a real number
  • is_numeric($a): Returns true if $a represents an integer or real number, or is a string representation of a number.

For example:

 
$a = 10;
$b = "10";
echo is_numeric($a);
echo "<br>";
echo is_numeric($b);

Both expressions is_numeric()will return true because $a represents a number and $b is a string representation of a number

  • is_bool($a) : returns a value true if $a holds a value true or false
  • is_scalar($a): Returns true if $a is one of the simple types: string, integer, real, boolean.
  • is_null($a) : returns a value true if $a holds a value null
  • is_array($a) : returns true if $a is an array
  • is_object($a) : returns true if $a contains a reference to an object

Type setting. settype() function

The settype() function can be used to set a variable to a specific type. It takes two parameters: settype(“Variable”, “Type”). The first parameter is the variable whose type is to be set, and the second parameter is the string description of the type returned by the function gettype().

If it was possible to set the type, then the function returns true, if not, then the value false.

For example, let’s set a variable to an integer type:

 
<?php
$a = 10.7;
settype($a, "integer");
echo $a; // 10
?>

Since the variable $a represents the real number 10.7, it is perfectly possible to convert it to an integer through decimal truncation. So in this case, the function will settype()return true.

Type conversion

By default, PHP automatically converts a variable’s value from one type to another when necessary. For this reason, explicit conversions in PHP are not often required. However, we can use them.

For explicit conversion, the type to which the conversion is to be performed is indicated in parentheses before the variable:

  
$boolVar = false;
$intVar = (int)$boolVar; // 0
echo "boolVar = $boolVar<br>intVar = $intVar";

In this case, the “false” value is converted into a value of type int, which will be stored in the variable $intVar. Namely, the value is false converted to the number 0. After that, we can use this value as a number.

When using an expression echo to display on a page, the passed values ​​are automatically converted to a string. And since the variable boolVaris false, its value will be converted to an empty string. Whereas the value 0 is converted to the string “0”.

In PHP, the following transformations can be applied:

  • (int), (integer): convert to int(integer)
  • (bool), (boolean): convert to bool
  • (float), (double), (real): convert to float
  • (string) : convert to string
  • (array) : convert to array
  • (object) : convert to object