May 10, 2021 PHP
Variables are "containers" used to store information:
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.
A variable is a container used to store data. |
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:
Both PHP statements and PHP variables are case sensitive. |
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! |
PHP does not have a command to declare a variable.
The variable is created the first time you assign it:
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.
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.
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:
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:
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.
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. |
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 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:
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:
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.
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:
We'll discuss it in more detail in the PHP Functions section.