What are variables in PHP 8?

Variables hold the value which can change during the execution of a program. PHP also contains variables like other languages however, it has its own set of rules to declare and use the variables in PHP 8. In this PHP 8 tutorial, you will learn about variables and data types in PHP.

To study what’s new in PHP 8, you can go to this link.

How to declare variables in php 8?

Variable is declared using $ sign followed by the name of the variable. If you try to declare the variable without a dollar sign, it will give a syntax error. Following is the example of declaring variables in PHP 8.

$current = 100;
$new = "foo";
$old = True;

One thing about PHP variables is really nice that is, there is no need to declare the data type along with the PHP variable. The variable automatically adopts the type of data stored in it.

$current = 100; // $current is of type integer
$new = "foo"; // $new is of type string
$old = True; // $old is type of boolean

How to convert variable from one data type to another?

As mentioned above, PHP variables show dynamic behavior in their type defining according to the data stored in them, but PHP developers enter is such situations where they need to convert the variable from one data type into another. Refer to the following example in which we declare a variable of type integer and then get its value as a string.

$int = 80; // $int is of type integer

$string = (string) $int; // $string is of type string, holding the value of $int but as a string

print($string) // outputs "80"

Set of rules to define variables in PHP 8

Every programming language defines certain rules for its variables. In this section, you will learn the rules to declare variables in PHP 8.

  • PHP variable must start with $ sign followed by the name of the variable.
  • PHP variables are case sensetive. $var and $VAR are two different variables.
  • Must name must start with an alphabet or underscore character.
  • Variable name cannot start with number, however it can contain number in the rest of locations.
  • Variable name contains only alphanumeric characters and underscore.

What are Local, Global and Static variables in PHP

Local, Global and Static are different scopes of PHP variables, that define the accessibility of the variables within the PHP code.

Global Variables

Global variables are available globally, anywhere in the current file in which you are working. However, you cannot access the global variables inside a function.

<?php
$x = 15; // $x has global scope

function testing() {
  // accessing $x in this function will give an error
  echo $x;
}
testing();

echo "x outside function: $x";
?>

Local Variables

Variables declared inside the function are known as local variables. They have scope inside the function in which they are declared. If you try to access them outside the function, it will give you an error.

<?php
function testing() {
  $x = 15; // $x has local scope
  echo "x inside function is: $x";
}
testing();
// accessing x outside the function will give an error
echo "x outside function is: $x";
?>

Static Variables

Static variables are used to store or maintain the state or a value until the program is not recompiled. Normally, PHP removes the variables declared in a function after the execution of the function is completed, but if the variable is static, it can be accessed even after the function has completed its execution.

Note: Static variables declared within the function have local scope.

<?php
function testing() {
  static $a = 10;
  echo $a;
  $a++;
}

testing(); output 11
testing(); output 12
testing(); output 13
?>

Tip: Variables in PHP that stores the address of the other variables are called reference variables.

GLOBALS array in PHP 8

GLOBALS is an array of variables that contains the globals variables. You can save and access the global variables from the GLOBALS array, even within the function. You can update the global variables inside the function by accessing it from the GLOBALS array. The following example will show you the concept more clearly.

<?php
$var1 = 15;
$var2 = 20;

function testing() {
  $GLOBALS['sum'] = $GLOBALS['var2'] + $GLOBALS['var1'];
}

testing();
echo $sum; // outputs 35
?>

Data types in PHP

There is a total of eight data types available in PHP 8.

Integer

Numeric value without a decimal point. Can be positive (+) or negative (-).

Floating point number or Float

Numeric value with a decimal point, therefore, known as floats, real numbers, or doubles.

String

Any character enclosed in single or double quotes. The maximum limit of string data size can be 2GB (2147483647 bytes).

Boolean

Can be either true or false.

Array

Store multiple values in a sequence inside the memory. The array can be one or multi-dimensional or can be associative. We will explore the arrays with more depth in the array tutorials.

Object

Objects are instances of their class and contain their properties. Objects not only store the value but also the information that tells how to process the data stored in it. We will learn about PHP object data type in its dedicated tutorial.

Resource

Resource data type variables are special variables holding the handler or reference to some external resource such as database connection or file object. We will study resources in PHP with more depth in its tutorial.

NULL

Null is a data type that only contains one value null. As result, it is one of the most interesting data types is both for the beginners as well as for experts.

One thing must be clear about NULL data type is following two things are always different from each other

$var = "" // is not null but an empty string

$var = NULL / is null

Note: A new variable contains a NULL value.

Tip: You can also store binary values in a variable. So, start the value with 0b followed by the binary value. However, to store hexadecimal value, start it with 0x, and to store octal start it with 0.