English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to store information in variables in PHP.
Variables are used to store data, such as text strings, numbers, etc. Variable values can be changed during the script process. Here are some important information about variables:
In PHP, there is no need to declare a variable before adding a value to it. PHP automatically converts the variable to the correct data type based on its value.
After declaring a variable, it can be reused throughout the entire code.
The assignment operator (=) is used to assign values to variables.
In PHP, variables can be declared as: \$var_name = value;
<?php //Declare variables \$txt = "Hello World!"; \$number = 10; //Display variable value echo \$txt; // Output: Hello World! echo \$number; // Output: 10 ?>Run and see‹/›
In the above example, we created two variables, the first of which assigned a string value, and the second of which assigned a number. Later, we used the echo statement to display the variable values in the browser. PHP echo statementIt is usually used to output data to the browser. We will learn more about this in the next chapter.
Scope can be defined as the range of availability of a variable to the program that declares it. PHP variables can be of four scope types.-
Variables declared within a function are considered local variables. That is, they can only be referenced within that function. Any assignment outside of the function will be considered as a completely different variable from the assignment within the function.-
<?php \$x = 4; function assignx () { \$x = 0; print "\$x inside the function is \$x.<br" />"; } assignx(); print "$x is outside the function as $x. <br />"; ?>Test to see ‹/›
Output result:
$x is inside the nested function as 0. $x is outside the function as 4.
Function parameters are declared after the function name and within parentheses. Their declaration is very similar to that of typical variables:
<?php //Multiply the value by10and then return it to the caller function multiply ($value) { $value = $value * 10; return $value; } $retval = multiply (10); Print "The return value is $retval\n"; ?>Test to see ‹/›
Output result:
The return value is 100
The last type of variable scope I discussed is called static. Unlike variables declared as function parameters (which are destroyed at the function exit), static variables do not lose their value when the function exits, and the value will still be retained if the function is called again.
You can declare a variable as static by simply placing the keyword STATIC before the variable name.
<?php function keep_track() { STATIC $count = 0; $count++; print $count; print "<br />"; } keep_track(); keep_track(); keep_track(); ?>Test to see ‹/›
Output result:
1 2 3
Unlike local variables, global variables can be accessed from any part of the program. However, to modify them, global variables must be explicitly declared as global variables in the function to be modified. This can be very conveniently achieved by placing the keyword global before the variable that should be recognized as global. Placing this keyword before an existing variable tells PHP to use the variable with that name. - Example
<?php $somevar = 15; function addit() { GLOBAL $somevar; $somevar++; print "Somevar =$somevar"; } addit(); ?>Test to see ‹/›
Output result:
Somevar = 16
These are the following rules for naming variables in PHP:
All variables in PHP start with $ A symbol followed by the variable name.
Variable names must start with a letter or an underscore (_) character.
Variable names cannot start with a number.
In PHP, variable names can only contain alphanumeric characters and underscores (A-z, 0-9and _).
Variable names cannot contain spaces.
Note: Variable names in PHPCase sensitiveThis indicates that $x and $X are two different variables. Therefore, one should be careful when defining variable names.