PHP Learning Summary Variables

Source: Internet
Author: User
Tags scalar variable scope
This article to you to share is about PHP learning variables part of the knowledge, the need for small partners can refer to

Variable identifiers

The identifier is the name of the variable, and the identifier for the variable in PHP has the following rules:

    • Consists of any letter, number, underscore, but cannot start with a number

    • Strictly case-sensitive

In order to scientifically standardize identifiers, there are several popular naming methods:

1. Hump Naming method

The first word starts with a lowercase letter, and the first letter of each word is capitalized (the small Hump method), or the first letter of each word is capitalized (the big hump method, also known as the Pascal Nomenclature).

$myFirstName = ' Jochen '; # Small Hump Method $myfirstname = ' Jochen '; # Big Hump method

2. Hungarian nomenclature

Variable name = attribute + Type + object description.

$sMyFirstName = ' Jochen ';

3. Underline naming method

All letters are lowercase, and each word is divided by an underscore

$my _first_name = ' Jochen ';

Assigning values to variables

PHP provides two ways to assign values to variables, namely, value assignment and reference assignment.

The most common is the value assignment, when the value of an expression is assigned to a variable, the value of the entire original expression is assigned to the target variable. This means that when the value of one variable is assigned to another variable, changing the value of one of the variables will not affect the other variable.

A reference assignment is an alias for the contents of the original variable, unlike a pointer in the C language, but changing the value of a variable affects the original variable.

$foo = ' Jochen '; # value Assignment $bar = & $foo;    # Reference Assignment $bar = ' Coco ';   # $foo, $bar values are Coco

Variable type

A variable type is a data type that can be saved in that variable. There are three types of data in PHP: scalar data types, composite data types, and special data types. PHP scalar data types are,,, boolean(布尔型) integer(整型) float(浮点型,也称作 double) string(字符串) , composite data types have array(数组) , object(对象) and callable(可调用) , special data types resource(资源) and NULL(无类型) .

PHP is a weakly typed language, and variables do not need to be declared before they are used, and the same variable can hold many different types of data. Sometimes, in order to determine the data type of the current variable, we can use the following method to judge:

GetType ()                 -Gets the type of the variable is_int () or Is_integer ()  -detects if the variable is an integer is_numeric ()              -detects if the variable is a numeric or numeric string is_float () or Is_ Double ()-detects if the variable is floating-point is_string ()-detects if the variable is a               string Is_bool ()                 -detects if the variable is a Boolean Is_array ()                -detects if the variable is an array is_object ()               -Detects if the variable is an object Is_null ()                 -detects if the variable is null is_resource ()             -detects if the variable is a resource type

Sometimes, we need to force a variable to be evaluated as a type, and a type cast will be necessary:

(int) or (integer)  -Convert to shape (bool) or (Boolean)-Convert to Boolean type (float) or (double)-Convert to float (string)-            Convert to String (array)             -Go Change to Array (object)            -Convert to Object (unset)             -Convert to NULL

Variable scope

1. Super Global variables

A variable that is predefined by PHP and is always available in any scope of any script. Hyper-Global variables include:

    • $GLOBALS

    • $_SERVER

    • $_REQUEST

    • $_POST

    • $_GET

    • $_FILES

    • $_ENV

    • $_COOKIE

    • $_SESSION

$GLOBALSIt is an associative array that contains all the global variables whose names are arrays key , and the values of the variables are arrays value .

2. Global variables

Variables created outside the function, statements, statements, and for while statements created in the statement are foreach also global variables, scoped from the statement that created the variable to the end of the file, but not inside the function. It is important to note that once a constant is created, it will be visible in the global and visible within the function.

3. Local Variables

A variable created inside a function whose scope is from the start of the statement that created the variable to the end of the function. It is important to note that if the variables inside the function are the same as the external global variable names, the internal variables will overwrite the external variables. In addition, a static variable created inside a function cannot be used outside the function.

requireAnd include does not affect the scope, that is, if the two statements function inside the function, the variable has a local scope, and if it is outside the function, the variable has global scope.

globaldeclaring a variable with a keyword can be used to manually specify that a variable defined or used in a function has a global scope.

Related recommendations:

PHP Learning Summary String

Related Article

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: info-contact@alibabacloud.com 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.