Variables for beginners to PHP

Variables in PHP are represented by a dollar sign followed by the variable name.

Variable names are case-sensitive.

Variable names follow the same rules as other tags in PHP.

A valid variable name starts with a letter or

or an underscore, followed by any number of letters, numbers, or underscores

Note: The letters mentioned here are a-z, A-Z, and ASCII characters from 127 to 255 (0x7f-0xff).

$this is a special variable that cannot be assigned a value

PHP variable naming rules

1. Variables start with the dollar sign $. Such as $name, $age.

2. The first character after the dollar sign $ cannot be a number, but can only be an underscore_ or a letter. Variables like $1_1 are wrong.

3. Except for underscore_, no spaces or punctuation marks are allowed in variables. That is to say, the variable name can only contain: a-z, A-Z, 0-9 and underscore_.

4. PHP variable names are case-sensitive. For example, $name and $Name are two different variables

The scope of the variable:

The scope of the variable is the part of the script where the variable can be referenced/used

localglobalstaticparameter

global keyword is used to access global variables within a function

PHP will all global Variables are stored in an array named $GLOBALS[index]. index holds the name of the variable. This array can be accessed inside the function or used directly to update global variables.

static Static variables only exist in the local function scope, but when the program execution leaves this scope, its value is not lost

Then, each time the function is called, the variable The value from the last time the function was called will be retained.

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

Parameter scope

Parameters are local variables whose values are passed to the function through the calling code.

Parameters are declared in the parameter list as part of the function declaration:


Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!