PHP Basics Tutorial-variables

Source: Internet
Author: User
Tags php basics variable scope alphanumeric characters

Variables are derived from mathematics and are abstract concepts that can store computational results or represent values in a computer language. Variables can be accessed by variable names. In an instruction language, variables are usually mutable, but in a purely functional language (such as Haskell), variables may be immutable (immutable). In some languages, variables can be explicitly defined as abstractions that can represent mutable states, with storage space, such as in Java and Visual Basic, but others may use other concepts, such as objects in C, to refer to this abstraction without strictly defining the exact extension of the "variable".

Instance
123456 <?php$x=5;$y=6;$z=$x+$y;echo$z;?>
Similar algebra
123 x=5y=6z=x+y

In algebra we use letters (such as x) to hold values (such as 5).

From the above expression Z=x+y, we can calculate the value of Z is 11.

In PHP, these three letters are called variables.

Note: Consider variables as containers for storing data.

PHP variables

As with algebra, PHP variables can be used to hold values (x=5) and Expressions (z=x+y).

The names of variables can be very short (such as x and y), or they can take more descriptive names (such as Carname, Total_volume).

PHP Variable rules:
    • The variable starts with the $ sign, followed by the name of the variable

    • Variable names must begin with a letter or underscore

    • Variable names cannot start with a number

    • Variable names can contain only alphanumeric characters and underscores (A-Z, 0-9, and _)

    • Variable names are case sensitive ($y and $Y are two different variables)

Note: The PHP variable name is case-sensitive!

Creating PHP Variables

PHP does not have a command to create a variable.

Variables are created the first time they are assigned a value:

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

After executing the above statement, the variable txt will save the value Hello world!, the variable x will hold the value 5, and the variable y will hold the value 10.5.

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

PHP is a loosely-typed language

In the example above, please note that we do not have to tell the data type of the PHP variable.

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

In languages such as C and C + + and Java, a programmer must declare its name and type before using a variable.

PHP variable Scope

In PHP, variables can be declared anywhere in the script.

The scope of a variable refers to the part of the script that a variable can be referenced/used.

PHP has three different scope of variables:

    • Local (partial)

    • Global (globally)

    • Static (statically)

Local and Global Scopes

Variables declared outside the function have Global scope and can only be accessed outside of the function.

Variables declared inside a function have a local scope and can only be accessed inside the function.

The following example tests for variables with local and global scopes:

Instance
12345678910111213141516 <?php$x=5; // 全局作用域function myTest() {  $y=10; // 局部作用域  echo"<p>测试函数内部的变量:</p>";  echo"变量 x 是:$x";  echo"<br>";  echo"变量 y 是:$x"; myTest();echo"<p>测试函数之外的变量:</p>";echo"变量 x 是:$x";echo"<br>";echo"变量 y 是:$x";?>

In the example above, there are two variables $x and $y, and a function myTest (). $x is a global variable because it is declared outside of a function, and $y is a local variable because it is declared within a function.

If we output a value of two variables inside the myTest () function, $y will output the locally declared value, but cannot $x value because it is created outside of the function.

Then, if you output a value of two variables outside of the myTest () function, the value of the $x is output, but the $y value is not output because it is a local variable and is created inside MyTest ().

Note: You can create local variables of the same name in different functions, because local variables can only be recognized by the function in which they are created.

PHP Global Keywords

The global keyword is used to access variables within the function.

To do this, use the Global keyword before (inside the function) variable:

Instance
1234567891011 <?php$x=5;$y=10;functionmyTest() {  global $x,$y;  $y=$x+$y;}myTest();echo$y// 输出 15?>

PHP also stores all global variables in an array named $GLOBALS [index]. The subscript contains the variable name. This array is also accessible within the function and can be used to update global variables directly.

The above example can be rewritten like this:

Instance
12345678910 <?php$x=5;$y=10;functionmyTest() {  $GLOBALS[‘y‘]=$GLOBALS[‘x‘]+$GLOBALS[‘y‘];myTest();echo$y// 输出 15?>
PHP Static Keywords

Typically, all variables are deleted when the function finishes/executes. However, sometimes I need to not delete a local variable. Achieving this requires a bit of further work.

To do this, use the static keyword when you first declare a variable:

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

Then, whenever the function is called, the information stored by the variable is the information contained in the last Call of the function.

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

PHP Basics Tutorial-variables

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.