PHP - Data types

  »   PHP Introduction  »   PHP Tutorial - Data types

PHP like most other programming languages supports various data types. It is very important to understand that variable cand act in a distinct way depending on the type of data they contain.

Here are all data types that PHP is using:

  • String - Simple text surrounded by commas
  • Integer - Mathematical whole numbers, without decimals
  • Float - Decimal or fractional numbers
  • Boolean - True/false values
  • Array - multiple values are stored inside this type of variable.
  • Object - It mainly stores information of how to process data
  • NULL - empty variable in PHP
  • Resource - it stores a reference to external functions and resources.

Now let's try to explain with more details and work with some real-world examples.

PHP strings

Strings are the most common type of data. They normally contain simple text or numbers as text. The simplest way to assign string values is to wrap it into single or double quotes.

php<?php
	$a = 'This is how you assign a simple string type value!';	 
	$b = "Another string type value using double quotes.";

	echo $a;
	echo "<br />";
	echo $b;
?>

Escaping quotes in strings

Simple or double quotes are lots of times found in blocks of text. To prevent code from breaking down we need to escape quotation marks like this

php$a = 'Here is some awesome code that I\'ll be writing to escape quotation marks.';
echo $a;

Other ways to define strings

There are also other ways to define string values. Besides single and double quotation you can use heredoc and nowdoc syntax if you run PHP 5.3.0 or later. Here is a heredoc example:

php$here = <<<EOT
    I'm here to show you how to use heredoc syntax!
EOT;

Nowdoc syntax similar to heredoc, but just like single quotes, it doesn't allow using and evaluating variable inside it.

Limits on string type data

According to the PHP manual: ( http://php.net/manual/en/language.types.string.php) "as of PHP 7.0.0, there are no particular restrictions regarding the length of a string on 64-bit builds. On 32-bit builds and in earlier versions, a string can be as large as up to 2GB (2147483647 bytes maximum)".

PHP integer data type

Integers are basically numbers without a decimal separator. You will normally use the decimal system to declare it but it is good to know that there are also other systems that can be used like hexadecimal (base 16 prefixed with 0x), octal (base 8 prefixed with 0) and since PHP 5.4+ you can also specify integers in binary (base 2 prefixed with 0b) system. You can also use the minus symbol (-) to define negative numbers

We will use the PHP var_dump build-in function to print out the variable type and value:

php$a = 3; // decimal number
var_dump($a) . PHP_EOL;
 
$b = -5; // a decimal negative number
var_dump($b) . PHP_EOL;
 
$c = 0x1F; // hexadecimal number 
var_dump($c) . PHP_EOL;
 
$d = 07; // octal number
var_dump($d) . PHP_EOL;

$d = 0b010; // binary number
var_dump($d) . PHP_EOL;

PHP float data type

Floats, doubles, or real numbers, are all the same and they all define numbers with a decimal separator or fractional numbers. This is the simplest form of a float point number:

php$a = 10.14; // float number
var_dump($a) . PHP_EOL;

PHP bolean data type

Boolean data is a very basic data type. It has only possible values either true or false. You can also use the equivalent, 1 and 0

php$is_home_page = true;
var_dump($is_home_page);

PHP array data type

The array data type is one of the most useful types of variable and it is normally used to store multiple values that are related to each other.

php$cryptocurrencies = array("Bitcoin", "Ethereum", "Ripple", "Litecoin");
var_dump($cryptocurrencies);

Arrays like this assume a numerical index for each value starting from 0. These are called indexed arrays and they offer you a way to directly access any values. They are very fast compared to other types of arrays, but this is the subject of another tutorial.

phpecho $cryptocurrencies[2]; // prints Ripple

Associative arrays

Associative arrays are arrays that you manually or dynamically set the keys of the values.

php$cryptocurrencies = array("BTC" => "Bitcoin", "ETH" => "Ethereum", "XRP" => "Ripple", "LTC" => "Litecoin");
var_dump($cryptocurrencies);
var_dump($cryptocurrencies["XRP"]);

To conclude, the array data type is more about structuring and storing data than about the type of data it stores on every key. It can contain strings, floats, boolean values or even multiples arrays (multidimensional arrays).

PHP object data type

Objects are more like a different way of programming. They are normally used to store functions that are later used to build data again and again.

php<?php
class user {

    public $name = "Nick";
    
    function print_user(){
        return $this->name;
    }
}
 
$my_user = new user;
var_dump($my_user);
?>

PHP NULL data type

NULL data type is the value that PHP assign to a variable when his value is not defined. Please note that NULL is not the same as an empty value.

php<?php
$not_setted_var;
var_dump($not_setted_var); // Thais has a value of NULL

$empty_var = "";
var_dump($empty_var); // this has a an empty value
?>

PHP resource data type

A resource is a special variable, holding a reference to an external resource like a database connection for example.

php$conn = mysqli_connect(localhost,"root","admin","users");

The resource data type is a much complex one, and we will return to it in a later tutorial