PHP is a dynamically typed language, which means that the data type of a variable is determined at runtime. PHP supports a wide range of data types, including scalar, compound, and special data types.
Scalar data types represent a single value. PHP supports four scalar data types:
<?php
$x = 42;
$y = -10;
$z = 0;
?>
<?php
$x = 3.14;
$y = -2.5;
?>
<?php
$name = 'John';
$message = "Hello, $name!";
?>
<?php
$x = true;
$y = false;
?>
Compound data types represent a collection of values. PHP supports two compound data types:
<?php
$fruits = array('apple', 'banana', 'orange');
$person = array('name' => 'John', 'age' => 30);
?>
<?php
class Person {
public $name;
public $age;
public function sayHello() {
echo "Hello, my name is $this->name!";
}
}
$person = new Person();
$person->name = 'John';
$person->age = 30;
$person->sayHello();
?>
Special data types represent special values. PHP supports two special data types:
<?php
$x = null;
?>
<?php
$file = fopen('example.txt', 'r');
?>
It is important to use the correct data type for each variable, as it can affect the performance and behavior of your code.
PHP supports a wide range of data types, including scalar, compound, and special data types. It is important to use the correct data type for each variable, as it can affect the performance and behavior of your code.