In PHP, variables do not need their data type to be declared. Let's find out how to declare a variable in this article.

I. Variables in PHP

1. What is a Variable in PHP?

A variable in PHP is the name of a memory location that contains data. A variable is a temporary storage used to store temporary data.

In PHP a variable starts with a $ sign, followed by the name of the variable.

Example

$txt = "Hello world!";
$x = 5;
$y = 10.5;

After executing the above statements, the variable $txt will hold the value Hello world!, the variable $x will hold the value 5, and the variable $y will hold the value 10.5.

Note:

  • When you assign a string value to a variable, remember to enclose the value in quotation marks.
  • In PHP, we don't need to declare the data type for variables, it will automatically assign the data type when we declare it. PHP automatically associates a data type with the variable, depending on its value.

2. Naming Rules for Variables in PHP

Naming convention for variables in PHP are as follows:

  • A variable starts with a $ sign, followed by the name of the variable.
  • Variable names must begin with a letter or an underscore character.
  • Variable names cannot begin with a number.
  • Variable names can only contain alphanumeric characters and underscores (A-z, 0-9, and _)
  • Variable names are case-sensitive ($age and $AGE are two different variables).

II. Data Types in PHP

PHP supports the following data types:

  • String
  • Integer
  • Float
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

1. Declaring Variable with String Data Type

The String data type is a series of characters, like "Hello world!".

When declaring a String type variable, you can place it within a set of double quotes " " or single quotes ' '.

Example

$x = "Hello world!";
$y = 'Hello world!';

echo $x;
echo "
";
echo $y;

2. Declaring Variable with Integer Data Type

The Integer data type is a data type of an integer in the range of -2,147,483,648 to 2,147,483,647.

Rules for integers:

  • An integer must have at least one digit.
  • An integer cannot 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 notation (base 2).

In the following example, $x is an integer. To check its data type, we will use the var_dump() function, which returns the data type and the value of the variable.

$x = 5985;
var_dump($x);

3. Declaring Variable with Float Data Type

The Float data type is a real number data type.

Example of declaring Float type

$x = 10.365;
var_dump($x);

4. Declaring Variable with Boolean Data Type

The Boolean data type represents either of two states: TRUE or FALSE.

Example

$x = true;
$y = false;

5. Declaring Variable with Array Data Type

The Array data type is a data type that stores multiple values in one variable.

Example

$cars = array("Volvo","BMW","Toyota");
var_dump($cars);

6. Declaring Variable with Object Data Type

The Object data type is a data type that stores data and information on how to process that data.

In PHP, an object must be explicitly declared. First, we must declare a class for the object.

For this, we use the keyword class. A class is a structure that can contain properties and methods.

To better understand, consider the following example

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 "
";
$myCar = new Car("red", "Toyota");
echo $myCar -> message();

In the above example, a class Car is declared with properties such as color, model. The variable names are in turn $color, $model.

We create a new Object that will inherit the properties of the class Car, however, the values ​​of color and model of the Object will receive the values ​​we declared when initializing the Object.

If you create a __construct() function, PHP will automatically call this function when you create an Object from a Class.

7. Declaring Variable with NULL Data Type

The NULL data type is a special data type in PHP that has only one value: NULL.

If a variable is declared but not assigned a value it will automatically be assigned the value NULL.

Example

$x = "Hello world!";
$x = null;
$y;
var_dump($x);
echo "
";
var_dump($y);

8. Declaring Resource Data Type

The Resource data type is a data type that does not belong to PHP, it is usually a data type referenced from functions or as a result of queries to the database.

III. Constants in PHP

A constant is also a variable but you cannot change its value. However, the way to declare a variable and a constant is different.

To create a constant, use the define() function.

The syntax to create is as follows

define(name, value)

Where:

  • name: The name of the constant
  • value: The value of the constant

Example

define("GREETING", "Welcome to kiemtientuweb.com!");
echo GREETING;

Note: Constants automatically have a global scope and can be used throughout the script.

If you declare a constant outside of a function, but inside the function you call the constant, it will still be available.

define("GREETING", "Welcome to kiemtientuweb.com!");

function myTest() {
  echo GREETING;
}
 
myTest();

 

Ngọc Phương

Web Developer

Thank you for visiting my blog. My name is Phuong and I have more than 10 years of experience in website development. I am confident in asserting myself as an expert in creating impressive and effective websites. Anyone in need of website design can contact me via Zalo at 0935040740.

0 feedback