Simple Introduction to PHP constants and variables

Source: Internet
Author: User
Tags echo name

This article gives you the content is about PHP constants and variables of the simple introduction, there is a certain reference value, the need for friends can refer to, I hope to help you.

Constant

1, constant, as the name implies is a normal measure
2. Constants are always constant during script execution
3, the definition and use of constants

Define a constant define (' name ', ' Wuhen ');//Use a constant echo name;//output Wuhenecho "<br>";//determine if the constant is defined var_dump (' name ' ));//result is bool (true)

Variable

1, a variable is used to temporarily store the value of a container, such as numbers, text characters, or arrays, etc.

2, the definition of variables
In PHP, a variable with a dollar sign ($) plus a variable name means that there is no need to display a declaration variable in PHP.

3. Naming rules

    • Variable names must begin with a letter or underscore "_"

    • Variable names can only contain letters, numbers, underscores

    • Variable names cannot contain spaces

    • PHP is a weak type check language, so variables need not be pre-defined before they are used, and no data type should be specified

4. Assigning values to variables
Value assignment: Use "=" To assign the value of an assignment expression directly to another variable
Reference assignment: Assigning a reference to an assignment's memory space to another variable

5, the destruction of variables
Use the unset () function

6. Determine if the variable exists
Use the Isset () function

Define a variable $a;//value-Assignment value $ A = 5;//reference assignment $b = & $a;//pass the address of $ A to $b$b = 6;echo $a;//result for 6//reference assignment, change $b equals change $a//destroy variable unset ($a);// Determine if the variable exists var_dump (isset ($a));//The result is bool (false), stating that $ A does not exist and has been destroyed

Scope of the variable

1. Local Variables
A variable declared inside a function whose scope is the function in which it is located. It's stored in the memory stack, so it's fast.

2. Global variables

    • As opposed to local variables, global variables can be accessed from anywhere in the program

    • Variables that are defined outside of all functions, whose scope is the entire PHP file

    • Use global variables inside the function, precede the variable with the keyword global declaration or use $global["] to access

Global variable $ A = 1;function FNC () {    //reference global variable    $a;        echo $a;        $a = $a +1;        Use a global variable array to refer to    echo $GLOBALS [' a '];} FNC ();//result is 1 2

3. Static variables

    • A static variable is a special local variable that exists only in the function scope.

    • The function is still present in the stack and will not be destroyed.

    • Add the keyword static before the variable, which becomes a static variable

Static variable function FNC () {    //defines a static variable    $count = 1;        echo $count;        $count + = 1;} FNC ();//result is 1FNC ();//Results for 2//description static variable $count, not destroyed as function ends

Variable variable

    • A mutable variable is the name of the variable that uses the value of a variable

    • Variable names can be dynamically named and used

    • The syntax is defined with two dollar sign $, or wrapped with {}.

Variable $ A = ' B '; $b = ' ABCDE '; echo $b;   echo $ $a; Equivalent to $becho ${$a}; Equivalent to $b//results are ABCDE

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.