PHP variables, constants learning notes _php Tips

Source: Internet
Author: User
Tags constant function definition


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. Using a pass address assignment, simply appends a (&) symbol to the variable that will be assigned the value (the source variable). This means that the new variable simply references the original variable, and 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;
?>



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 some variables in PHP in the scope of a program does not need the global declaration, these variables are called super Global variables, and these hyper-global variables are basically not user-defined, but some 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 is no sequential relationship here, but instead uses ${$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;
?>



In addition, how to output a constant and a value of a variable, which involves a string operation of PHP, uses the English period (.) to combine string concatenation into a new string, similar to the & in ASP.

echo myname. ",". $MYNAME; Output is "Cnbruce,cnrose"


Like predefined variables in a variable, PHP also has predefined constants (or magic constants) that do not require the define () function definition. Like what

__FILE__ represents the full path and file name of the file, similar to the Server.MapPath current file in ASP


<?php
Echo __file__;
?>



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