PHP Tutorial



DATA TYPES IN PHP


📚 PHP Data Types

In PHP, variables can hold different types of data. PHP automatically converts the variable to the correct data type based on its value.

Main data types in PHP:

  • String – sequence of characters
  • Integer – whole numbers
  • Float (Double) – decimal numbers
  • Boolean – true or false
  • Array – multiple values in one variable
  • Object – instance of a class
  • NULL – empty variable

🔹 Examples of Each Type

<?php
  $string = "Hello World";      // String
  $int = 100;                   // Integer
  $float = 99.99;               // Float
  $bool = true;                 // Boolean
  $array = array("HTML", "CSS", "PHP");  // Array
  $null = NULL;                 // NULL
?>
  

📘 1. String

Text enclosed in quotes:

$text = "Technorank is awesome!";

🔢 2. Integer

Whole numbers without decimal:

$age = 21;

🌡️ 3. Float

Numbers with decimals:

$temperature = 36.6;

✅ 4. Boolean

Returns either true or false:

$is_active = true;

📦 5. Array

Stores multiple values:

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

🎯 6. Object

Used in Object-Oriented Programming (OOP):

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

🚫 7. NULL

A variable with no value assigned:

$x = NULL;
💡 Tip: PHP is a loosely typed language, so you don't need to declare the type of a variable.
✅ Summary: PHP supports various data types including strings, numbers, booleans, arrays, objects, and null. PHP automatically identifies the type based on the assigned value.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review