Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PhP variable


May 10, 2021 PHP


Table of contents


PhP variable

A variable is a number used in a program that can be changed, as opposed to a constant that, once defined, cannot be changed.

Variables are "containers" used to store information:

<?php
$x=5;
$y=6;
$z=$x+$y;
echo $z;
?>

Run an instance . . .

Similar to algege generals

x=5
y=6
z=x+y

In algege while using a letter such as x, we assign it a value (such as 5).

From the above expression z-x-y, we can calculate that the value of z is 11.

In PHP, these letters are called variables.

PhP variable A variable is a container used to store data.


PhP variable

Similar to algege for an algeon, you can give a PHP variable a value (x-5) or an expression (z-x-y).

Variables can be short names (such as x and y) or more descriptive names (such as age, carname, totalvolume).

PHP variable rules:

  • The variable starts with the $ symbol, followed by the name of the variable
  • The variable name must start with a letter or underscore character
  • Variable names can only contain alphanumeric characters and underscores (A-z, 0-9, and .
  • The variable name cannot contain spaces
  • The variable name is case sensitive ($y and $Y are two different variables)
PhP variable Both PHP statements and PHP variables are case sensitive.
PhP variable Although PHP variable names can be underlined, we don't recommend that you do, because variables that start with the following dashes in PHP are actually usually the variables that come with the system!


Create (declare) PHP variables

PHP does not have a command to declare a variable.

The variable is created the first time you assign it:

<?php
$txt="Hello world!";
$x=5;
$y=10.5;
?>

Run an instance . . .

In the execution of the statement above, the variable txt saves the value Hello world! and the variable x saves the value 5.

Note: When you assign a text value to a variable, quote both sides of the text value.


PHP is a weak type language

In the example above, we noticed that the data type of the variable does not have to be declared to PHP.

PHP automatically converts variables to the correct data type based on their value.

In a strong-type programming language, we must declare (define) the type and name of the variable before using it.


PhP variable scope

The scope of a variable is the part of the script in which the variable can be referenced/used.

PHP has four different variable scopes:

  • local
  • global
  • static
  • parameter

Local and global scopes

Variables defined outside all functions have a global scope. In addition to functions, global variables can be accessed by any part of the script, and the global keyword is required to access a global variable in a function.

The variable declared inside the PHP function is a local variable that can only be accessed inside the function:

<?php
$ x = 5; // global variable

function myTest()
{
$ y = 10; // Local variable
echo "<p>Test variables inside the function:<p>";
echo "Variable x is: $x";
echo "<br>";
echo "Variable y is: $y";
}

myTest();

echo "<p>Test variables outside the function:<p>";
echo "Variable x is: $x";
echo "<br>";
echo "Variable y is: $y";
?>

Run an instance . . .

In the above example, the myTest() function defines the $x and $y variables. $x variable is declared outside the function, so it is a global variable, $y variable is declared within the function so it is a local variable.

When we call the myTest() function and output the value of two variables, the function outputs the value of the local variable $y, but cannot output the value of $x because the $x variable is defined outside the function and cannot be used within the function, using the global keyword if you want to access a global variable in a function.

Then we output the values of two variables outside the myTest() function, which outputs the value of the global part variable $x, but not the value of $y, because the $y variable is defined in the function and belongs to a local variable.

PhP variable You can use the same variable name in different functions because the variable names defined within these functions are local and only work within the function.

PHP global keyword

The global keyword is used to access global variables within a function.

To call a global variable defined outside the function, we need to add the global keyword before the variable in the function:

<?php
$x=5;
$y=10;

function myTest()
{
global $x,$y;
$y=$x+$y;
}

myTest();
echo $y; O utput 15
?>

Run an instance . . .

PHP stores all global variables in an $GLOBALS called "index" . olds the name of the variable. This array can be accessed inside the function or used directly to update global variables.

The above example can be written like this:

<?php
$x=5;
$y=10;

function myTest()
{
$GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];
}

myTest();
echo $y;
?>

Run an instance . . .


Static scope

When a function is complete, all its variables are usually deleted. However, sometimes you want a local variable not to be deleted.

To do this, use the static keyword the first time you declare a variable:

<?php

function myTest()
{
static $x=0;
echo $x;
$x++;
}

myTest();
myTest();
myTest();

?>

Run an instance . . .

Then, each time the function is called, the variable retains the value at the time the function was previously called.

Note: The variable is still a local variable of the function.


The parameter scope

An argument is a local variable that passes values to a function by calling code.

Arguments are declared in the argument list as part of a function declaration:

<?php

function myTest($x)
{
echo $x;
}

myTest(5);

?>

We'll discuss it in more detail in the PHP Functions section.