PHP variable and constant learning NOTE _ php tips-php Tutorial

Source: Internet
Author: User
Keep in touch with PHP variables and constant learning notes (read notes from the PHP Manual). a dollar sign ($) in PHP is followed by a variable name, indicating a variable. Variable names are case sensitive. The variable name can start with a number or underline. 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.


$ 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, rather than global or local variables when variables are defined at the first line of the program and assigned values.


$ 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 ".


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.


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.