Skip to main content

PHP Data Types

By SamK
0
0 recommends
Category(s)
Topic(s)

PHP Data types are the types of data, which PHP Variables can store. PHP supports the following data types:

  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL

Getting the Data Type

You can determine the data type of any object by utilizing the var_dump() function.

<?php
$var = 5;
var_dump($var);
?>

PHP Getting Data Type Demo

Now we'll look at each data type with an example.

1. PHP String

String is basically a piece of text or a sequence of characters, like "Hello world!". Strings need to be wrapped in either " " or ' '. Any HTML tag or code snippet is also considered a string in PHP. For example:

<?php
$hello = 'Hello World!';
$how = "How are you?";
var_dump($hello);
echo '<br/>';
var_dump($how);
?>

The above example will display the data type (string) and value of the PHP variables $hello and  $how, as you can see below.

PHP String Demo

2. PHP Integer

Any non-decimal number between -2,147,483,648 and 2,147,483,647 is considered an Integer in PHP.  An integer must have at least one digit, which can be either positive or negative. Integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base 8), and binary (base 2) notations. Integers are used without " " or ' ' in PHP statements. Like:

<?php
$x = 1234;
var_dump($x);
?>

The above example will display the data type (integer) and value of the PHP variable $x , as you can see below.

PHP Integer Demo

3. PHP Float

A floating point number (PHP float) is a number either in a decimal form or an exponential form. Float is also called a double. Floats are used without " " or ' ' in PHP statements. Like:

<?php
$x = 12.34;
var_dump($x);
?>

The above example will display the data type (float) and value of the PHP variable $x , as you can see below.

PHP Float Demo

4. PHP Boolean

A Boolean data type represents either of the two possible states:

  • TRUE or FALSE
  • 1 or 0

Boolean is mostly used in conditional testing and without " " or ' ' in PHP statements. , like:

<?php
$x = true;
$y = false;
var_dump($x);
echo '<br>';
var_dump($y);
?>

The above example will display the data type (boolean) and value of the PHP variables $x and $y , as you can see below.

PHP Boolean Demo

5. PHP Array

Arrays are used to store multiple values in one single variable. In the example below, $names is an array and it is storing 3 values (names) in it. Arrays are mostly used for grouped data like a set of data in a person's profile or a collection of fruits.

<?php
$names = array("Sami","David","Ali");
print_r ($names);
?> 

The above example will display the data type (array) and value of the PHP variable $names , as you can see below.

PHP Array Demo

6. PHP Object

A PHP object is a type of data, which stores the data and information on how to process that data. To use PHP objects, first we must declare a class of object using the class keyword. A class is a structure that can contain properties and methods: In PHP, an object must be explicitly declared.

 In the below example, Fruit is a class and color is the property.

<?php
// Define the Fruit class
class Fruit {
    public $color;  // Property to store color
    
    // Constructor to initialize color property
    public function __construct() {
        $this->color = "Yellow";
    }
}

// Create an object (instance) of the Fruit class
$mango = new Fruit();

// Output the color property of the $mango object
echo "<p>Color of the mango: " . $mango->color . "</p>";
?>

The above example will output the color property of the $mango object as shown below.

PHP Object Demo

7.PHP NULL Value

Null is a unique data type that can only have one value: NULL.

A NULL variable is a variable that has no value assigned to it.

Note: If a variable is declared without an initial value, it is automatically assigned NULL.

Variables can also be cleared by setting their value to NULL.

<?php
$x = "Hello world!";
$x = NULL;
var_dump($x);
?>

The above example will display the data type (null) and value of the PHP variable $x , as you can see below.

PHP Null Demo

Change Data Type

In PHP, variables are dynamically typed. This means that the type of a variable is automatically determined based on the value assigned to it. For example, if you assign an integer value to a variable, it will be of type integer. If you then assign a string value to the same variable, its type will change to string.

<?php
$x = 12;
var_dump($x);
echo "<br>";
$x = "Hello World!";
var_dump($x);
?>

Change Data Type Demo

In the above example:

  • Initially, $x is assigned an integer value (12), so its type is int.
  • After reassignment with the string "Hello World!", the type of $x changes to string.

Questions & Answers