PHP variables, constants learning notes

Source: Internet
Author: User
Tags constant first row

close contact with PHP variables, constant learning notes (read notes from PHP manual)

A dollar sign ($) in PHP follows a variable name, representing a variable. The name of the variable is case sensitive. where the variable name allow numbers or underscores to begin

to assign a value to a variable's pass address

In PHP 3, variables are always assigned values. PHP 4 provides another way to assign a value to a variable: to send an address assignment. Use Send address Assignment, that is, simply append A (&) symbol to the variable that will be assigned the value(source variable). This means that the new variable simply references the original variable, changing the new variable will affect the original variableVice versa.


<?php
$foo = ' Bob ';
$bar = & $foo;
$bar = "My name is $bar";
Echo $bar;
Echo $foo;
?>


Variable foo is only assigned in the first row and normally output as "Bob", however, the value of variable foo changes as the variable bar value changes, but the assignment is assigned to the variable bar.

about (Super) global variables

The declaration of a PHP global variable is declared when the variable is referenced, not as a global or a local variable when the first line of the program is defined or assigned.


<?php
$a = 1;
$b = 2;

function Sum ()
{
Global $a, $b;
$b = $a + $b;
}

Sum ();
Echo $b;
?>



If the global variable is not declared using global in the function sum (), the program complains about the undefined variable.

Of course, there are variables in PHP that do not require global declarations within the scope of a program's functionality, which are called Super global variable, and these hyper-global variables are basically not user-defined, but PHP predefined variables, such as $_get, $_post, $_cookie, and so on.

about variable variables

The more interesting variable variables in PHP, such as $a = "Bruce", can also be expressed as $bruce by using $ $a, variable is the two dollar sign used.

But in $ $a [1], is $a [1] as a variable, or $ $a as a variable and take out the value indexed by [1] in that variable? There 's no back-and-forth relationship here., but instead uses the ${$a [1]} or ${$a}[1] to represent both of these situations.

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

about Constants

Constants are distinguished from variables, and their scope is global by definition of constants.

Volume defaults to case sensitivity, which is always capitalized according to the Convention constant identifier

Constants not preceded by dollar sign ($)

Constants cannot be redefined or undefined once they are defined

Constants can only be defined with the Define () function, not by assignment statements

For example, define ("MyName", "cnbruce") is defined as a MyName constant with a value of "Cnbruce"


<?php
Define ("MyName", "cnbruce");
$MYNAME = "Cnrose";
Echo myname;
Echo $MYNAME;
?

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.