Naming rules for php variables and usage of php variables (with code)

不言
Release: 2023-04-03 14:32:01
Original
10338 people have browsed it

What are the uses of php variables? PHP variables are "containers" used to store information. PHP variables can be assigned values ​​or expressions. So, let's take a look at the naming rules of PHP variables and the usage of PHP variables.

Recommended Manual: php complete self-study manual

Let’s look at an example first:

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

and Similar to algebra, you can assign a certain value (x=5) or an expression (z=x y) to a PHP variable.

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

PHP variable naming rules:

Variables start 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 _)

Variable names cannot contain spaces

Variable names are case-sensitive ($ y and $Y are two different variables)

PHP variables do not need to be declared, they are created when they are first assigned.

<?php
$x="this is a string"
$y=7
$z=6.0
?>
Copy after login

Note: When you assign a text value to a variable, please add quotation marks around the text value.

PHP is a weakly typed language

In the above example, we noticed that it is not necessary to declare the data type of the variable to PHP.

PHP will automatically convert the variable into the correct data type based on its value.

In a strongly typed 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 where the variable can be referenced/used.

PHP has four different variable scopes:

local

global

static

parameter

php local variables and global variables

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

Variables declared inside a PHP function are local variables and can only be accessed inside the function:

 <?php
$x=5; // 全局变量

function myTest()
{
    $y=10; // 局部变量
    echo "<p>测试函数内变量:<p>";
    echo "变量 x 为: $x";
    echo "<br>";
    echo "变量 y 为: $y";
} 

myTest();

echo "<p>测试函数外变量:<p>";
echo "变量 x 为: $x";
echo "<br>";
echo "变量 y 为: $y";
?>
Copy after login

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

When we call the myTest() function and output the values ​​​​of two variables, the function will output 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 Used within a function, if you want to access a global variable in a function, you need to use the global keyword.

Then we output the values ​​of the two variables outside the myTest() function. The function will output the value of all local variables $x, but cannot output the value of $y because the $y variable is defined in the function. Belongs to local variables.

PHP global keyword

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

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

<?php
$x=5;
$y=10;
 
function myTest()
{
    global $x,$y;
    $y=$x+$y;
}
 
myTest();
echo $y; // 输出 15
?>
Copy after login

PHP stores all global variables in a file called $GLOBALS [index] in the array. index holds 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[&#39;y&#39;]=$GLOBALS[&#39;x&#39;]+$GLOBALS[&#39;y&#39;];
} 
 
myTest();
echo $y;
?>
Copy after login

Static scope

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

To do this, use static when you first declare the variable Keywords:

<?php
function myTest()
{
    static $x=0;
    echo $x;
    $x++;
}
 
myTest();
myTest();
myTest();
?>
Copy after login

Summary:

  • Global variables are defined outside the function, and their scope extends from the point of definition to the end of the file.

  • Variables defined within a function are local variables, and their scope is within the scope of the function definition.

  • There are scopes between functions that do not affect each other.

  • To access global variables within a function, you need the global keyword or use the $GLOBALS[index] array

Function has an independent role in PHP domain, so local variables will overwrite global variables. Even if the same variable is defined in the local variable and does not have a global variable, it will be overwritten.

Recommended related articles:
1.php variable naming rules
2.Introducing PHP file naming, class and method naming, variable naming and other specifications
3.Naming rules for php function names
Related video recommendations:
1.Dugu Jiujian (4)_PHP video tutorial

The above is the detailed content of Naming rules for php variables and usage of php variables (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!