Variables in PHP

Source: Internet
Author: User

Basis

Variables in PHP are represented by a dollar sign followed by a variable name. Variable names are case-sensitive.

The variable name follows the same rules as other tags in PHP. A valid variable name begins with a letter or underscore, followed by any number of letters, numbers, or underscores. According to the normal regular expression, it will be expressed as: '[a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*'.

Note: The letters mentioned here are a-z,a-z, as well as ASCII characters from 127 to 255 (0x7f-0xff).

Note: $this is a special variable, it cannot be assigned a value.

Tip

Please see User space naming guide .


The function information about the variable is shown in the variable function.

<?php$var =  ' Bob '; $Var  =  ' Joe ' ;echo  "$var,  $Var";      //  output   "Bob, joe" $4site =   ' Not yet ';     //  illegal variable name; $_4site =  ' Not yet ' with a digital start;     //  legal variable name; $i site is =  ' Mansikka ';   //  the legal variable name ; can be used in Chinese; 

php also provides another way to assign a value to a variable: reference assignment. This means that the new variable is a simple reference (in other words, "becomes its alias" or "points to") the original variable. Changing the new variable will affect the original variable and vice versa.

using reference assignment, simply add a & symbol to the variable that will be assigned (source variable).

For example, the following code fragment will output "My name is Bob" two times:

<?php$foo =  ' Bob ';               //  assign   ' Bob '   to   $foo $bar  = & $foo;               //   through   $bar   references   $foo $bar =  "my name is  $bar";  //  modified   $bar   variable echo  $bar;echo  $foo;                  //  $foo   Values have also been modified? 

It is important to point out that only a variable with a name can reference the assignment.

<?php$foo = $bar = & $foo;  Legal Assignment $bar = & (24 * 7); Illegal Reference an expression with no Name function test () {return 25;}    $bar = &test (); Illegal?>

Although it is not necessary to initialize variables in PHP, it is a good practice to initialize variables. Uninitialized variables have default values of their type-the variable default value for a Boolean type is FALSE, the default values for shaping and floating-point variables are zero, string variables ( For example ,EchoThe default value is an empty string and the default value for the array variable is an empty array.


Variables in PHP

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.