


Naming rules for php variables and usage of php variables (with code)
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; ?>
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 ?>
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"; ?>
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 ?>
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['y']=$GLOBALS['x']+$GLOBALS['y']; } myTest(); echo $y; ?>
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(); ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In PHP development, we often encounter the error message PHPNotice:Undefinedvariable. This error message means that we have used an undefined variable in the code. Although this error message will not cause the code to crash, it will affect the readability and maintainability of the code. Below, this article will introduce you to some methods to solve this error. 1. Use the error_reporting(E_ALL) function during the development process. In PHP development, we can

How to use numeric variables in PHP In PHP, a numeric variable is a variable type that is used directly without declaration. You can use numeric variables to perform mathematical calculations, data comparisons, and other numerical operations. This article will explain how to use numeric variables in PHP and provide specific code examples. Defining numeric variables In PHP, defining numeric variables is very simple, just assign a number directly to the variable. Here is an example: $number=10; In the above code, we define a value called $numb

Solution to PHPNotice:Undefinedvariable:arrin In PHP programming, we often encounter the error message "Notice:Undefinedvariable". This error message is usually caused by accessing an undefined variable or the variable has not been initialized. For this problem, we need to find the problem and solve it in time. In this article, we will focus on PHPNotice:Undefin

How to quickly eliminate PHP variable undefined errors? In PHP development, we often encounter undefined variable errors. This is because an unassigned variable is used in the code. When encountering this kind of error, we need to quickly find the cause of the error and solve it. Here are some ways to quickly troubleshoot PHP variable undefined errors to help you locate and fix errors faster. Turn on error reporting: When we turn on error reporting, PHP will display all error and warning messages, including variable undefined errors. We can do this by opening the code

When developing a PHP application, if you encounter the "Undefined variable: sql" prompt, it usually means that you are referencing an undefined variable. This can be due to many reasons, such as misspellings of variable names, scoping issues, or syntax errors in the code, etc. In this article, we will explore the various causes of this problem and provide some ways to solve it. 1. Variable name is misspelled In your PHP code, if the variable name is incorrect or misspelled, the system

In PHP, you can use the ampersand (&) symbol to pass variables by reference instead of by value. This allows the original variable to be modified within a function or method. There are mainly two ways to pass PHP variables by reference: Using ampersand symbol Using ampersand symbol in function/method declaration Using ampersand symbol in function/method declaration When passing variables to function/method In PHP, you can use function/ The ampersand symbol (&) in a method declaration passes variables by reference. Here is the updated explanation: To pass a reference variable by using the & symbol in a function/method declaration, you need to include the & symbol before the parameter name in the function/method definition. This indicates that parameters should be passed by reference, allowing

PHPNotice:Undefinedvariable:result means that an undefined variable result is called in the PHP program, which will cause the program to generate a Notice-level warning. This situation is generally caused by programmers not correctly defining variables or the scope of variables when writing PHP code. If not resolved in time, this Notice level warning may cause problems in the operation of the program. So, how to resolve PHPNotice:

In PHP programming, variables are the basic unit of storing values and are used to store and use data during program execution. In PHP, variables can be assigned different data types, including integers, floating point, strings, arrays, etc. In this article, we will introduce common variables and their usage in PHP programming. Simple variables are the most common variable type. They can store regular data types such as integers, floating point numbers, and strings. In PHP, the initial value of undefined variables is NULL. Here are a few examples: Integer variable: $
