Home > Backend Development > PHP Tutorial > PHP variables and constants study notes_PHP tutorial

PHP variables and constants study notes_PHP tutorial

WBOY
Release: 2016-07-21 15:53:04
Original
831 people have browsed it



Assignment by address of variables

In PHP 3, variables are always assigned by value. PHP 4 provides another way to assign values ​​to variables: assignment by address. To use pass-by-address assignment, simply append an ampersand (&) to the variable to be assigned (the source variable). This means that the new variable simply references the original variable, and changes to the new variable will affect the original variable, and vice versa.


$foo = 'Bob';
$bar = &$foo;
$bar = "My name is $bar";
echo $bar;
echo $foo; Given the variable bar, when the value of variable bar changes, the value of variable foo also changes.

About (super) global variables

The declaration of PHP global variables is declared when the variable is referenced, rather than when defining or assigning the variable in the first line of the program to define whether it is a global or local variable.


$a = 1;
$b = 2;

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

Sum();
echo $b;
?>




If global variables are not declared using global in the function Sum(), the program will report an undefined variable error.

Of course, there are some variables in PHP that do not require global declaration within the functional scope of a certain program. These variables are called superglobal variables, and these superglobal variables are basically not user-defined, but Some variables predefined by PHP, such as $_GET, $_POST, $_COOKIE, etc.

About variable variables

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

But in $$a[1] , is $a[1] used as a variable, or $$a is used as a variable and the value of index [1] in the variable is taken out? There is no sequential relationship here, but ${$a[1]} or ${$a}[1] is used to represent the above two situations.

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

About constants

Constants are different from variables. From the moment a constant is defined, its scope is the global

Quantity default They are case-sensitive, and by convention constant identifiers are always uppercase

There is no dollar sign ($) in front of the constant

Once a constant is defined, it cannot be redefined or undefined

Constants can only be defined using the define() function, not through assignment statements

For example, define("MYNAME","cnbruce") defines a MYNAME constant with the value "cnbruce"


define("MYNAME","cnbruce");
$MYNAME="cnrose";
echo MYNAME;
echo $MYNAME;
?> ;



In addition, how to output the values ​​of constants and variables together requires PHP string operations. Use English periods (.) to merge string connections into new ones. String, similar to & in ASP.

echo MYNAME.",".$MYNAME; The output is "cnbruce,cnrose"


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

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


echo __FILE__;
?> ;



PHP predefined constants are divided into:
Kernel predefined constants, constants defined in the PHP kernel, Zend and SAPI modules
Standard predefined constants, in PHP Default defined constants




http://www.bkjia.com/PHPjc/318766.html

www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/318766.htmlTechArticleAbout variable assignment by address In PHP3, variables are always assigned by value. PHP4 provides another way to assign values ​​to variables: assignment by address. Use pass-by-address assignment, i.e. simply append a...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template