PHP variables explained in detail

Source: Internet
Author: User
Tags variable scope alphanumeric characters

A variable is a "container" for storing information.

Defines the syntax for a variable:

$ variable name = value;

Examples of using variables:

<?php$x=5; $y =6; $z = $x + $y; echo $z;? >

Run

As you can see from this example, the variable name starts with $ and indicates that it is a variable. The variable name begins with a letter (A-Z, A-Z) or an underscore, followed by any letter or number and an underscore, but not a space.

PHP Variable rules:

    • The variable starts with the $ sign, followed by the name of the variable
    • Variable names must start with a letter or underscore character
    • Variable names can contain only 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)

Note: Both the PHP statement and the PHP variable are case-sensitive.

These variable names are valid as follows:

$var _CHAR$VARCHAR$_VARCHAR$VAR_CHAR5
Create (declare) PHP variables

PHP does not have a command to declare variables.

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

<?php$txt= "Hello world!"; $x =5; $y = 10.5;? >

In the above statement execution, the variable txt will hold the value Hello world!, and the variable x will hold the value 5.

Note: When you assign a text value to a variable, enclose the text value in quotation marks.

PHP is a weakly typed language

In the above example, we notice that you do not have to declare the data type of the variable to PHP.

PHP automatically converts variables 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 it.

PHP Variable Scope

The scope of a variable is the part of the script in which variables can be referenced/used.

PHP has four different scope of variables:

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

Variables defined outside of all functions, with 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, and to use the Global keyword.

The variables declared inside the PHP function are local variables and can only be accessed inside the function:

<?php$x=5; Global variable function myTest () {$y = 10;//local variable echo "<p>test variables inside the function:<p>"; echo "Variable x is: $ X "echo" <br> "echo" Variable y is: $y ";} MyTest (); echo "<p>test variables outside the function:<p>"; echo "Variable x is: $x"; echo "<br>"; echo "V Ariable y is: $y ";? >

In the above example, the MyTest () function defines $x and $y variables. $x variable is declared outside the function, so it is a global variable, $y variable is declared inside the function so it is a local variable.

When we call the MyTest () function and output the value of two variables, the function will output the value of the local variable $y, but cannot output the value of the $x because $x variable is defined outside the function and cannot be used within the function, if you want to access a global variable in a function, you need to use the Global keyword.

Then we output the value of two variables outside of the mytest () function, which will output the value of the global part variable $x, but cannot output $y value because $y variable is defined in the function and belongs to the local variable.

Note: You can use the same variable names in different functions, because the variable names defined within these functions are local variables and only work within that function.

PHP Global Keywords

The global keyword is used within a function to access globally variable.

To invoke a global variable defined outside a function within a function, we need to add the global keyword before the variable in the function:

<?php$x=5; $y =10;function myTest () {global $x, $y; $y = $x + $y;} MyTest (); Echo $y; Output 15?>

PHP stores all global variables in an array called $GLOBALS [index]. Index holds the name of the variable. This array can be accessed inside the function, or it can be used to update global variables directly.

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 is complete, all its variables are usually deleted. However, there are times when you want a local variable not to be deleted.

To do this, use the static keyword When you declare a variable for the first time:

<?phpfunction myTest () {static $x =0;echo $x; $x + +;} MyTest (); MyTest (); MyTest ();? >

Then, each time the function is called, the variable retains the value of the function before it was called.

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 code.

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

<?phpfunction MyTest ($x) {echo $x;} MyTest (5);? >

Original address: http://www.manongjc.com/php/php_variables.html

Read about PHP:

PHP Str_pad () function to complement string to specified length

PHP str_replace () string substitution function usage analysis and explanation

The PHP str_ireplace () function does not match the characters in a case-insensitive replacement string

The PHP str_getcsv () function parses a CSV string into an array

PHP sscanf () function using methods and examples to explain

PHP variables explained in detail

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.