PHP Data Types (With Examples)

By Shahnail Khan Last updated : December 1, 2023

PHP Data Types

Variables in PHP are capable of storing various types of data, each categorized into eight distinct data types. These include basic, compound, and special data types, each serving a specific purpose. You have basic data types like Boolean, Integer, Double, and String.

Additionally, PHP supports user-defined or compound data types, specifically Arrays and Objects. There are also special data types in PHP, namely NULL and resource.

PHP Data Types

This figure gives us a clear picture of PHP Data types.

Different Types of PHP Data Types

PHP has the following data types -

PHP Basic Data Types

Under basic data types, we have -

  • Integers
  • Floats (Floating-Point Numbers)
  • Strings
  • Booleans

PHP Compound Data Types

Under compound data types, we have-

  • Arrays
  • Objects

PHP Special Data Types

Under special data types, we have-

  • Resource
  • NULL

PHP Integers

In PHP, integers are used to represent whole numbers, including both positive and negative values, without any fractional or decimal part. They can be expressed in three bases: decimal (base 10), octal (base 8), or hexadecimal (base 16), with the default base being decimal.

  • Decimal Integers:
    $decimal_integer = 42;
    
  • Octal Integers:
    $octal_integer = 052; // Leading 0 denotes octal
    
  • Hexadecimal Integers:
    $hexadecimal_integer = 0x2A; // Leading 0x denotes hexadecimal
    

Example

This example demonstrates the PHP integer type.

<?php
$decimal_integer = 42;
$octal_integer = 052;
$hexadecimal_integer = 0x2a;

echo "The value of decimal_integer is: $decimal_integer<br/>";
echo "The value of octal_integer is: $octal_integer<br/>";
echo "The value of hexadecimal_integer is: $hexadecimal_integer<br/>";

// Printing values and types
var_dump($decimal_integer);
echo "<br/>";
var_dump($octal_integer);
echo "<br/>";
var_dump($hexadecimal_integer);
?>

Output:

The value of decimal_integer is: 42
The value of octal_integer is: 42
The value of hexadecimal_integer is: 42
int(42)
int(42)
int(42)

PHP Floats

In PHP, floats are versatile data types that can represent numbers with fractional or decimal parts, including both positive and negative values. They can also express numbers in exponential form.

Decimal and Exponential Floats:

$decimal_float = 3.14;
$scientific_notation = 2.3e4; // 2.3 * 10^4

Example

This example demonstrates the PHP float data type.

<?php
$decimal_float = 3.14;
$scientific_notation = 2.3e4;

echo "The first value is: $decimal_float<br/>";
echo "The second value is: $scientific_notation.<br/>";

// Printing value and type
var_dump($decimal_float);
echo "<br/>";
var_dump($scientific_notation);
?>

Output:

The first value is: 3.14
The second value is: 23000.
float(3.14)
float(23000)

PHP Strings

Strings store sequences of characters, including letters and numbers. Use double quotes for flexibility and single quotes for literal values.

  • Double-Quoted String:
    $double_quoted_string = "Hello, World!";
    
  • Single-Quoted String:
    $single_quoted_string = 'PHP allows single-quoted strings.';
    

Note: Understanding the difference between single and double quotes is important, especially when dealing with variable values within strings.

Example

This example demonstrates the PHP string data type.

<?php
$str1 = 'Hello, world!';
$str2 = "Welcome at IncludeHelp";

echo "The first string is: $str1.<br/>";
echo "The second string is: $str2.<br/>";

// Printing value, size, and type
var_dump($str1);
echo "<br/>";
var_dump($str2);
?>

Output:

The first string is: Hello, world!.
The second string is: Welcome at IncludeHelp.
string(13) "Hello, world!"
string(22) "Welcome at IncludeHelp"

PHP Booleans

Booleans in PHP are fundamental for conditional testing, holding either TRUE (equivalent to 1) or FALSE (equivalent to 0). Successful events typically return TRUE, while unsuccessful events return FALSE. Additionally:

  • NULL Values:
    $null_value = NULL; // Treated as FALSE in boolean
    
  • Zero Values:
    $zero_value = 0; // Also considered FALSE in boolean
    
  • Empty Strings:
    $empty_string = ''; // Treated as FALSE in Boolean
    

Example

This example demonstrates the PHP Boolean data type.

<?php
$x = true;
if ($x) {
    echo "It's true.";
} else {
    echo "It's false.";
}

echo "<br/>";

$x = null;
if ($x) {
    echo "It's true.";
} else {
    echo "It's false.";
}

echo "<br/>";

$x = "Hello";
if ($x) {
    echo "It's true.";
} else {
    echo "It's false.";
}
?>

Output:

It's true.
It's false.
It's true.

PHP Arrays

Arrays, a compound data type in PHP, efficiently store multiple values of the same data type.

They are like containers for holding multiple pieces of information. Here's an example with integers:

$integer_array = [1, 2, 3, 4, 5];

Example

This example demonstrates the PHP array data type.

<?php
$numbers = [1, 2, 3, 4, 5];

echo "First element: $numbers[0]<br/>";
echo "Second element: $numbers[1]<br/>";
echo "Third element: $numbers[2]<br/>";
echo "Fourth element: $numbers[3]<br/>";
echo "Fifth element: $numbers[4]<br/><br/>";

// Printing array's value and type
var_dump($numbers);
?>

Output:

First element: 1
Second element: 2
Third element: 3
Fourth element: 4
Fifth element: 5

array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) }

PHP Objects

Objects in PHP are instances of user-defined classes. They encapsulate both data and functions specific to the class. Objects inherit properties and behaviors from the class, each instance having unique property values.

Let's take an example to understand objects better.

Example

<?php
class Car
{
    public $brand;
    public $model;
}
// Creating an instance of the Car class
$my_car = new Car(); 
$my_car->brand = "Toyota";
$my_car->model = "Camry";
echo $my_car->brand . "<br>";
echo $my_car->model;
?>

Output:

Toyota
Camry

In this example, we define a Car class with public properties $brand and $model. We then create an instance of the Car class named $my_car and set its properties to specific values. After creating the $my_car object, we demonstrate how to access its properties ($brand and $model) using the arrow (->) notation.

PHP NULL

NULL in PHP is a special variable type that can only hold one value: NULL. It is case-sensitive, typically written in capital letters. When a variable is created without a value, or explicitly set to NULL, it automatically takes on this special value.

Let's understand NULL by this example.

Example

<?php
$variable = null;
echo "The variable is $variable <br>";

if (is_null($variable)) {
    echo "The variable is NULL";
}
?>

Output:

The variable is
The variable is NULL

In this example, we can see that the value of $variable is "NULL". It can be seen in the output that the first echo statement didn't display the $variable value.

PHP Resources

Resources in PHP are used to store references, often to external functions or resources, such as database connections. They are not an exact data type but serve as handles for specific operations like file handling or database connections.

Let's take an example to demonstrate all the datatypes in PHP.

<?php
$integer_variable = 42;
$float_variable = 3.14;
$string_variable = "Hello, PHP!";
$boolean_variable = true;
$array_variable = [1, 2, 3];
$object_variable = new stdClass();
$null_variable = null;

echo "Integer: $integer_variable <br>";
echo "Float: $float_variable<br>";
echo "String: $string_variable<br>";
echo "Boolean: " . ($boolean_variable ? "true" : "false") . "<br>";
echo "Array: " . print_r($array_variable, true) . "<br>";
echo "Object: " . print_r($object_variable, true) . "<br>";
echo "NULL: " . var_export($null_variable, true) . "<br>";
?>

Output:

Integer: 42
Float: 3.14
String: Hello, PHP!
Boolean: true
Array: Array ( [0] => 1 [1] => 2 [2] => 3 )
Object: stdClass Object ( )
NULL: NULL

This example demonstrates the use of various PHP data types, including integers, floats, strings, booleans, arrays, objects and NULL.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.