php data type

PHP Data Type

Variables are used to store data and each variable can store one type of data. PHP Data type is one thing that beginner programmers don’t deal with but need to become a professional.

In defining the variables, we said that when defining the variable, PHP defines the data type of the variable automatically and we don’t need to define it. In the following, we will discuss the types of data types in PHP.

PHP supports the following data types:

  • String
  • Integer
  • Float (یا double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

String data type in PHP

The string data type is used to store strings and text.

$str = 'Honar Systems';
echo $str;
----------------
Honar Systems

Learn more about PHP string.

Integer PHP data type

This data type is an integer between -2,147,483,648 and 2,147,483,647.

  • An integer must have at least one digit
  • An integer must not contain a decimal
  • An integer can be positive or negative
  • Integers can be specified as decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2).
$var = 1989;
echo $var;
------------
1989

Float

This data type is a simple decimal or exponential number.

$var=1989.56;
var_dump($var);
--------------
float 1989.56

Boolean

This data type is set to true or false.

$var1 = true;
$var2 = false;

Array

If you have more than one value, you can store it in an array variable. An array is a variable that stores multiple values.

$cars = array("Ferrari","Benz","BMW","Volvo",100,true);

Learn more about arrays in PHP.

Object

This type of data is used to store an object of the class.

class Car {
    public $color;
    public $name;
    public function __construct($color, $name) {
      $this->color = $color;
      $this->name = $name;
    }
    public function message() {
      return "My car is a " . $this->color . " " . $this->name . "!";
    }
}
  
$myCar = new Car("black", "Ferrari");
echo $myCar -> message();
echo "<br>";
$myCar = new Car("red", "Benz");
echo $myCar -> message();
-----------------------
My car is a black Ferrari!
My car is a red Benz!

NULL

NULL data type is a data type that stores only NULL value. In fact, this data type declares that the variable has no data to store.

If a variable is defined but its value is not specified, its value will automatically be NULL.

$var;
var_dump($var);
---------------
null

Resource

This data type is not actually a real data type and you are referring to a resource such as a database variable. We will refer to this type of data more in other articles.

6546546s4fas6df46sd

Shopping Cart