PHP Data Types


PHP data types determine the specific nature of the data stored within a variable. PHP automatically determines variable types based on assigned values since it operates as a dynamically typed language without the need for explicit type declarations.

php-datatypes

PHP Data Types

String

Single or double quotes hold a series of characters in PHP.

 

$name = "Alice";
echo "Hello, $name!"; 

Integer

Whole numbers (positive or negative).

 

$age = 25;
$year = -2024;

Integer

Whole numbers (positive or negative).

 

$age = 25;
$year = -2024;

Float (Double)

Decimal numbers.

$price = 19.99;
$pi = 3.1416;

Boolean

Represents either true or false.

 

$isActive = true;
$isLoggedIn = false;

Array

Holds multiple values in a single variable..

 

$colors = ["Red", "Green", "Blue"];
echo $colors[0];  // Output: Red

Object

An instance of a user-defined class.

 

class Car {
    public $brand = "Toyota";
}
$myCar = new Car();
echo $myCar->brand;

Example

<?php
// 1. String
$name = "Alice";
echo "String: $name<br>";   // Output: Alice

// 2. Integer
$age = 30;
echo "Integer: $age<br>";   // Output: 30

// 3. Float
$price = 99.99;
echo "Float: $price<br>";   // Output: 99.99

// 4. Boolean
$isAdmin = true;
echo "Boolean: " . ($isAdmin ? 'true' : 'false') . "<br>"; // Output: true

// 5. Array
$colors = ["Red", "Green", "Blue"];
echo "Array: " . $colors[0] . "<br>";   // Output: Red

// 6. Object
class Car {
    public $brand = "Toyota";
}
$car = new Car();
echo "Object: " . $car->brand . "<br>";  // Output: Toyota

// 7. NULL
$data = null;
echo "NULL: ";
var_dump($data);  // Output: NULL
?> 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.