PHP Data Types


PHP Data Types

Variables can store data of different types, and different types of data can do different things.

PHP supports the following types of data:

  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

PHP String

The alphabet unit is a sequence of letters, such as "Hello world!".

The character unit can be any text within the quotes. You can use one or two quotes:


Example
<?php
$x = "Hello world!";
$y = 'Hello world!';

echo $x;
echo "<br>";
echo $y;
?>


PHP Integer

The total data is a non-decimal number between -2,147,483,648 and 2,147,483,647.

Rules for integers:

  • An integer must have at least one digit
  • An integer must not have a decimal point
  • An integer can be either positive or negative
  • Integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation

In the following example $ x is a whole number. The PHP function var_dump () returns the data type and value:


Example
<?php
$x = 5985;
var_dump($x);
?>


PHP Float

A float (a floating point number) is a number with a decimal point or a number in the descriptor form.

In the following example $ x is float. The PHP function var_dump () returns the data type and value:


Example
<?php
$x = 10.365;
var_dump($x);
?>

PHP Boolean

Boolean represents two possible situations: TRUE or FALSE.


$x = true;
$y = false;

Booleans are often used in conditional testing. You will learn more about conditional testing in the next chapter of this study.


PHP Array

The list saves multiple values ​​for a single variable.

In the following example $ cars is the same member. The PHP function var_dump () returns the data type and value:


Example
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>


PHP Object

Classes and objects are two main elements of an object-focused program.

A classroom is a model of objects, and an object is a model of a class.

When each item is created, it inherits all the features and behaviors in the classroom, but each item will have different value structures.

Suppose we have a class called Car. The car can have properties such as model, color, etc. We can define variables like $ model, $ color, etc., to capture the values ​​of these properties.

When individual items (Volvo, BMW, Toyota, etc.) are created, they inherit all the buildings and behaviors from the classroom, but each item will have different property values.

When you create a __construct () function, PHP will call this function automatically when you create an object from the classroom.


Example
<?php
class Car {
  public $color;
  public $model;
  public function __construct($color, $model) {
    $this->color = $color;
    $this->model = $model;
  }
  public function message() {
    return "My car is a " . $this->color . " " . $this->model . "!";
  }
}

$myCar = new Car("black", "Volvo");
echo $myCar -> message();
echo "<br>";
$myCar = new Car("red", "Toyota");
echo $myCar -> message();
?>


PHP NULL Value

Null is a special data type that can have only one value: NULL.

The NULL data type variable is a variable without a given value.

Tip: If the exception is created out of value, it is automatically given NULL value.

Variables can also be specified by setting the value to NULL:


Example
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>


PHP Resource

Special service type is not actual data type. Maintaining reference for activities and resources outside of PHP.

A common example of using a data center application is a website.

We will not talk about the type of service here, as it is an advanced topic.