Naming rules for PHP variables and usage of PHP variables (with code)

Source: Internet
Author: User
Tags alphanumeric characters
What is the use of PHP variables? PHP variables are "containers" for storing information, PHP variables can be assigned values or expressions, so let's take a look at the naming conventions for PHP variables and the use of PHP variables.

Let's look at an example:

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

Similar to algebra, you can assign a value (x=5) or an expression (z=x+y) to a PHP variable.

Variables can be very short names (such as x and y) or more descriptive names (such as age, Carname, Totalvolume).

PHP variable naming 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)

The PHP variable does not need to be declared, it is created at the time of the first assignment.

<?php$x= "This is a string" $y =7$z=6.0?>

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

PHP Local variables and global variables

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 function internal variable:<p>";    echo "Variable x is: $x";    echo "<br>";    echo "Variable y is: $y";} MyTest (); echo "<p> test function outside variable:<p>"; echo "variable x is: $x"; echo "<br>" echo "Variable y: $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.

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 ();? >

Summary:

    • Defined outside the function is a global variable whose scope is defined at the end of the file.

    • A variable defined within a function is a local variable scoped to a function definition.

    • There are scopes between functions that do not affect each other.

    • Accessing global variables within functions requires the Global keyword or using the $GLOBALS [index] Array

In PHP, functions have separate scopes, so local variables overwrite global variables, even if they are in local variables and define variables that do not have the same global variables, they are overwritten.

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.