Lesson 5: PHP Data Types

الدرس 5 : PHP Data Types

Lesson 5: PHP Data Types

When writing any PHP program, you’ll deal with different types of data. Each data type serves a specific purpose and helps structure your code better. In this article, we’ll explore the basic PHP data types with practical examples.


1. String

Strings are sequences of characters. You can create a string in PHP using single or double quotes.

$name = "Alaa"; 
$message = 'Welcome!';

You can concatenate strings using the dot operator:

$fullMessage = $message . " " . $name;

2. Integer

Integers represent whole numbers without decimals.

$age = 30; 
$year = 2025;

3. Float (or Double)

This data type is used for numbers with decimal points.

$price = 19.99; 
$rating = 4.8;

4. Boolean

Boolean values are either true or false. They're commonly used in conditions.

$is_logged_in = true; 
$is_admin = false;

5. Array

Arrays allow you to store multiple values in a single variable.

$colors = ["Red", "Green", "Blue"];

You can access any element using its index:

echo $colors[1]; // Output: Green

6. Object

Objects are used in object-oriented programming. They are instances of classes.

class Person { public $name; } 
$person = new Person(); 
$person->name = "Alaa";

7. Null

A variable with the value null has no value assigned.

$job = null;

Summary

PHP supports a wide range of data types to meet different programming needs. A solid understanding of these types will help you write clean, well-organized code. Each type has a use case, and knowing when to use each one improves your development quality.

Important links Portfolio

Share with your friends

العودة إلى المقالات