Core PHP

Lesson 4: Basic Data Types

Overview

This lesson explains the four basic data types of PHP and how their conversions are handled: boolean, int, floating-point, and string. When a variable is displayed inside a string, the value of the variable is automatically converted to a string type. Automatic conversions occur in PHP under other various circumstances, like arithmetic operations, and we must understand how and when those occur. Lastly, we can cause controlled type conversions with type casts.

The Four Data Types

There are four basic data types in PHP: boolean, integer, floating-point, and string. The boolean data type is used represent logical values and can only take on the values true and false. The integer data type takes on integer values and is typically used for counting in loops. The floating-point data type is used to represent real number values and is used for mathematical calculations. Finally, the string data type is a set a of characters and is used to represent text messages.

In the program below, I have created a variable of each type. When coding, I prepend a lowercase letter to my variable names to signal the type of data that the variable holds: b. for boolean, i for integer, f. for floating-point, and s fo string. After creating the variables and assigning them values in the program below, I use the var_dump() function to print their types and values. Note that the var_dump() function records the type with the shortened names: bool, int, and float.

Prog1.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php

$bIsDone = true;
$iLoopCount = 10;
$fPi = 3.14159;
$sName = 'XoaX.net';

echo "<pre>";
var_dump($bIsDone);
var_dump($iLoopCount);
var_dump($fPi);
var_dump($sName);
echo "</pre>";

?>
  </body>
</html>
 
 

Output for Prog1.php

 

Automatic Type Conversion to Strings

In this program, I replaced the boolean from the last program with two others to illustrate how both possible values are handled. Below the variable declarations, I output each variable inside a string and check its type with the gettype() function. The prupose of this is to demonstrate the when variables are printed in a string, the type is changed to a string automatically. Note that this conversion has some strange effects for the boolean values: true is converted to 1 and false is converted to an empty string. This is somewhat unexpected behavior.

Prog2.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php

$bIsTrue = true;
$bIsFalse = false;
$iLoopCount = 10;
$fPi = 3.14159;
$sName = 'XoaX.net';

echo "<pre>";
echo 'The converted value of $bIsTrue as a '.gettype("$bIsTrue").' is '."$bIsTrue".'.<br />';
echo 'The converted value of $bIsFalse as a '.gettype("$bIsFalse").' is '."$bIsFalse".'.<br />';
echo 'The converted value of $iLoopCount as a '.gettype("$iLoopCount").' is '."$iLoopCount"
  .'.<br />';
echo 'The converted value of $fPi as a '.gettype("$fPi").' is '."$fPi".'.<br />';
echo 'The converted value of $sName as a '.gettype("$sName").' is '."$sName".'.<br />';
echo "</pre>";

?>
  </body>
</html>
 
 

Output for Prog2.php

 

Automatic Arithmetic Conversions and Alternative Type Names

One of the other potential problems that we want to be aware of is the automatic type conversion that PHP does for arithmetic operators. In this program, I declare three variables and illustrate how their data types are handled with various arithmetic operators. First note that the gettype() function uses slightly different names for the data types: boolean instead of bool, integer instead of int, and double instead of float.

As far as the type conversions, there are some important and possibly unexpected things to notice. We see that multiplying an int by a float results in a float type. Multiplying two strings results in an integer because the values inside the strings are integers. Multiplying the string by a float gives a float. Multiplying the string by an int gives and int.

At the end, I perform some divisions inside var_dump() calls to illustrate how division is handled. Note that the integer 2 divided by the integer 3 yields a float value; this is different from other languages that would give and integer value of 0. On the other haner hand, 12 divided by 3 remains an intger type because the division is even. However, 4.5 divided by 1.5 remains a float value, even though the result is an integer.

Prog3.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php

$iPersons = 3;
$fSqrtTwo = 1.41421;
$sFollowers = '12';

echo "<pre>";
echo 'The type of true: '.gettype(true).'.<br />';
echo 'The type of $iPersons * $iPersons: '.gettype($iPersons * $iPersons).'.<br />';
echo 'The type of $fSqrtTwo * $fSqrtTwo: '.gettype($fSqrtTwo * $fSqrtTwo).'.<br />';
echo 'The type of $sFollowers * $sFollowers: '.gettype($sFollowers * $sFollowers).'.<br />';
echo 'The type of $iPersons * $fSqrtTwo: '.gettype($iPersons * $fSqrtTwo).'.<br />';
echo 'The type of $fSqrtTwo * $sFollowers: '.gettype($fSqrtTwo * $sFollowers).'.<br />';
echo 'The type of $sFollowers * $iPersons: '.gettype($sFollowers * $iPersons).'.<br />';
var_dump($fSqrtTwo * $fSqrtTwo);
var_dump(2 / 3);
var_dump(12 / 3);
var_dump(4.5 / 1.5);
echo "</pre>";

?>
  </body>
</html>
 
 

Output for Prog3.php

 

Type Casts

This last program is used to illustrate that we can force changes to data types by using a type cast. To do that, put the name of the new type inside parentheses and prepend it to the value as shown. When we do this, integer values convert to float types even though the value remains an integer. The float values chop off their fractional part when converted to an integer. Boolean values of true convert to 1 and false values convert to 0 or an empty string.

Prog4.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php

$iDays = 7;
$fPhi = 1.61803;
$sLength = '8.52 ft';

echo '<pre>';
echo 'Converting $iDays to float: '.(float)$iDays.'.<br />';
echo 'Converting $fPhi to int: '.(int)$fPhi.'.<br />';
echo 'Converting $sLength to int: '.(int)$sLength.'.<br />';
echo 'Converting $sLength to float: '.(float)$sLength.'.<br /><br />';

echo 'Converting $iDays to bool: '.(bool)$iDays.'.<br />';
echo 'Converting $fPhi to bool: '.(bool)$fPhi.'.<br />';
echo 'Converting $sLength to bool: '.(bool)$sLength.'.<br /><br />';

echo 'Converting true to int: '.(int)true.'.<br />';
echo 'Converting true to float: '.(float)true.'.<br />';
echo 'Converting true to string: '.(string)true.'.<br />';
echo 'Converting false to int: '.(int)false.'.<br />';
echo 'Converting false to float: '.(float)false.'.<br />';
echo 'Converting false to string: '.(string)false.'.<br />';
echo '</pre>';

?>
  </body>
</html>
 
 

Output for Prog4.php

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.