Php variable details, php variable _ PHP Tutorial

Source: Internet
Author: User
Php variables are described in detail. Php variables are used to store information. syntax for defining a variable: $ variable name value. example of using a variable :? Php $ x5; $ y6; $ z $ x + $ y; echo $ z ;? Php variables

A variable is a "container" used to store information ".

Syntax for defining a variable:

$ Variable name = value;

Example of using variables:



From this example, we can see that the variable name starts with $, indicating that this is a variable. Variable names start with letters (a-z, A-Z) or underscores (_), followed by any letter or number and underline, but not a space.

PHP variable rules:

  • The variable starts with the $ symbol, followed by the variable name.
  • The variable name must start with a letter or underscore.
  • Variable names can only contain letters, numbers, and underscores (A-z, 0-9, and _)
  • Variable names cannot contain spaces
  • Variable names are case sensitive ($ y and $ Y are two different variables)

Note: PHP statements and PHP variables are case sensitive.

The following variable names are valid:

Create (declare) PHP variables

PHP has no command to declare variables.

The variable is created when you assign it a value for the first time:


In the preceding statement executionTxtSave valueHello world!And variableXSave value5.

Note:When you assign a text value to a variable, enclose it with quotation marks.

PHP is a weak language

In the above example, we noticed that the data type of the variable does not need to be declared to PHP.

PHP automatically converts the variable to the correct data type based on the value of the variable.

In a strongly typed programming language, we must declare (define) the type and name of the variable before using the variable.

PHP variable scope

The scope of a variable is a part of the script that can be referenced or used.

PHP has four different variable scopes:

  • Local
  • Global
  • Static
  • Parameter
Local and global scopes

Variables defined externally in all functions have a global scope. Except for functions, global variables can be accessed by any part of the script. to access a global variable in a function, you must use the global keyword.

The variable declared in the PHP function is a local variable and can only be accessed within the function:

 Test variables inside the function:

"; Echo" Variable x is: $ x "; echo"
"; Echo" Variable y is: $ y ";} myTest (); echo"

Test variables outside the function:

"; Echo" Variable x is: $ x "; echo"
"; Echo" Variable y is: $ y ";?>

In the above example, the myTest () function defines the $ x and $ y variables. $ X is declared outside the function, so it is a global variable. $ y is declared in 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, it cannot be used in the 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 two variables outside the myTest () function. the function will output the value of $ x, but cannot output the value of $ y, because the $ y variable is defined in the function, it is a local variable.

Note:You can use the same variable name in different functions, because the variable names defined in these functions are local variables and only apply to these functions.

PHP global keyword

The global keyword is used to access global variables in the 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 a table named $ GLOBALS [Index.IndexSave the variable name. This array can be accessed inside the function or used directly to update global variables.

The above instance can be written as follows:


Static scope

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

To do this, useStaticKeywords:


Then, each time the function is called, the variable retains the value of the previous call of the function.

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

Parameter scope

A parameter is a local variable that passes a value to a function by calling the code.

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



Php Related reading:

Php str_pad () function to populate the string with the specified length

Analysis and explanation of php str_replace () string replacement function

The php str_ireplace () function replaces characters in strings with case-insensitive characters.

The php str_getcsv () function parses the CSV string into an array.

Php sscanf () function usage and example

Container variable is the "container" used to store information. syntax for defining a variable: $ variable name = value; example of using a variable :? Php $ x = 5; $ y = 6; $ z = $ x + $ y; echo $ z ;?...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.