PHP Tutorials
PHP Forms
PHP Advanced
PHP OOP
Variables can store data of different types, and different types of data can do different things.
PHP supports the following types of data:
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:
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
The total data is a non-decimal number between -2,147,483,648 and 2,147,483,647.
Rules for integers:
In the following example $ x is a whole number. The PHP function var_dump () returns the data type and value:
<?php
$x = 5985;
var_dump($x);
?>
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:
<?php
$x = 10.365;
var_dump($x);
?>
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.
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:
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
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.
<?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();
?>
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:
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
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.