PHP variables and constant learning notes

Source: Internet
Author: User

Assignment of variable transfer addresses

In PHP 3, values are always assigned to variables. PHP 4 provides another way to assign values to variables: assign values to input addresses. Assign values using the transfer address, that is, simply append a (&) symbol to the variable to be assigned (source variable ). This means that the new variable simply references the original variable. Changing the new variable will affect the original variable, and vice versa.

<? PHP
$ Foo = 'bob ';
$ Bar = & $ Foo;
$ Bar = "My name is $ bar ";
Echo $ bar;
Echo $ Foo;
?>

The Foo variable is assigned only to the first line. Normally, it should be output as "Bob". However, the address is assigned to the variable bar. When the value of the variable bar changes, the value of the foo variable also changes.

About (Super) global variables

PHP global variables are declared when variables are referenced, ratherProgramWhen defining and assigning values to variables in the first line, you can define global or local variables.

<? PHP
$ A = 1;
$ B = 2;

Function sum ()
{
Global $ A, $ B;
$ B = $ A + $ B;
}

Sum ();
Echo $ B;
?>

If global variables are not declared in sum (), the program reports an error for undefined variables.

Of course, some variables in PHP do not require global declaration within the scope of a program. These variables are called Super global variables, and these super global variables are basically not user-defined, some predefined PHP variables, such as $ _ Get, $ _ post, and $ _ cookie.

Variable

Variables that are interesting in PHP, such as $ A = "Bruce", can also be represented as $ Bruce using $ A, that is, variables are two dollar symbols used.

But in $ A [1], is $ A [1] A variable, or is $ A used as a variable and the value cited as [1] In this variable retrieved? There is no sequential relationship between the preceding and following. Instead, $ {$ A [1]} or $ {$ A} [1] is used to represent the preceding two cases.

========================================================== ========================

Constant

A constant is different from a variable, and its range is global.

By default, constant identifiers are case sensitive.

No dollar sign before a constant ($)

Once defined, a constant cannot be redefined or undefined.

Constants can only be defined using the define () function, but cannot be defined using the value assignment statement.

For example, define ("myname", "cnbruce") defines a myname constant with a value of "cnbruce ".

<? PHP
Define ("myname", "cnbruce ");
$ Myname = "cnrose ";
Echo myname;
Echo $ myname;
?>

In addition, how to output the constant and variable values together involves the PHP string operation. You can use the English periods (.) to combine the string connection into a new string, similar to the & in ASP &.

Echo myname. ",". $ myname; the output is "cnbruce, cnrose"

Like predefined variables in variables, PHP also has predefined constants (or magic constants), that is, the define () function definition is not required. For example

_ File _ indicates the complete path and file name of the file, which is similar to the current file server. mappath in ASP.

<? PHP
Echo _ file __;
?>

PHP predefined constants include:
Kernel pre-defined constants defined in the PHP kernel, Zend, and SAPI modules
Standard predefined constants, which are defined by default 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.