³¬Ô½PHP PHP¶¯Ì¬ | ¾­µäÎÄÕ | CLASS | Ïà¹ØÏÂÔØ | ³£¼ûÎÊÌâ | FORUM | WIKI | ÔÚÏßÊÖ²á
Site search:    
<ÀàÐÍÏ··¨Ô¤¶¨Òå±äÁ¿>
Last updated: Fri, 22 Jun 2007

Õ 12. ±äÁ¿

»ù´¡

PHP ÖÐÒ»¸öÃÀÔª·ûºÅºóÃæ¸úÉÏÒ»¸ö±äÁ¿Ãû³Æ£¬¼´±íʾһ¸ö±äÁ¿¡£±äÁ¿µÄÃû³ÆÊǶԴóСдÃô¸ÐµÄ¡£

±äÁ¿ÃûÓë PHP ÖÐÆäËüµÄ±êǩһÑù×ñÑ­ÏàͬµÄ¹æÔò¡£Ò»¸öÓÐЧµÄ±äÁ¿ÃûÓÉ×Öĸ»òÕßÏ»®Ïß¿ªÍ·£¬ºóÃæ¸úÉÏÈÎÒâÊýÁ¿µÄ×Öĸ£¬Êý×Ö£¬»òÕßÏ»®Ïß¡£°´ÕÕÕý³£µÄÕýÔò±í´ïʽ£¬Ëü½«±»±íÊöΪ£º'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

×¢: ×ÖĸΪ a-z£¬A-Z£¬ASCII ×Ö·û´Ó 127 µ½ 255£¨0x7f-0xff£©¡£

<?php
$var
= "Bob";
$Var = "Joe";
echo
"$var, $Var";      // outputs "Bob, Joe"

$4site = 'not yet';    // invalid; starts with a number
$_4site = 'not yet';    // valid; starts with an underscore
$t?yte = 'mansikka';    // valid; 'ä§ is (Extended) ASCII 228.
?>

PHP 3 ÖУ¬±äÁ¿×ÜÊÇ´«Öµ¸³Öµ¡£ÄÇÒ²¾ÍÊÇ˵£¬µ±Ä㽫һ¸ö±í´ïʽµÄÖµ¸³ÓèÒ»¸ö±äÁ¿Ê±£¬Õû¸öԭʼ±í´ïʽµÄÖµ±»¸³Öµµ½Ä¿±ê±äÁ¿¡£ÕâÒâζ×Å£¬ÀýÈ磬µ±Ò»¸ö±äÁ¿µÄÖµ¸³ÓèÁíÍâÒ»¸ö±äÁ¿Ê±£¬¸Ä±äÆäÖÐÒ»¸ö±äÁ¿µÄÖµ£¬½«²»»áÓ°Ïìµ½ÁíÍâÒ»¸ö±äÁ¿¡£ÓйØÕâÖÖÀàÐ͵ĸ³Öµ²Ù×÷£¬Çë²ÎÔıí´ïʽһÕ¡£

PHP 4 ÌṩÁËÁíÍâÒ»ÖÖ·½Ê½¸ø±äÁ¿¸³Öµ£º´«µØÖ·¸³Öµ¡£ÕâÒâζ×ÅеıäÁ¿¼òµ¥µÄÒýÓ㨻»ÑÔÖ®£¬¡°³ÉΪÆä±ðÃû¡± »òÕß ¡°Ö¸Ïò¡±£©ÁËԭʼ±äÁ¿¡£¸Ä¶¯ÐµıäÁ¿½«Ó°Ï쵽ԭʼ±äÁ¿£¬·´Ö®ÒàÈ»¡£ÕâͬÑùÒâζ×ÅÆäÖÐûÓÐÖ´Ðи´ÖƲÙ×÷£»Òò¶ø£¬ÕâÖÖ¸³Öµ²Ù×÷¸ü¼Ó¿ìËÙ¡£¾¡¹ÜÈç´Ë£¬ÈκÎÌáËٵIJÙ×÷Ö»ÓÐÔÚ½ôÃÜÑ­»·»òÕß´óÊý×é»òÕß¶ÔÏó²Å¿ÉÄܱ»×¢Òâµ½¡£

ʹÓô«µØÖ·¸³Öµ£¬¼òµ¥µØ×·¼ÓÒ»¸ö£¨&£©·ûºÅµ½½«Òª¸³ÖµµÄ±äÁ¿Ç°£¨Ô´±äÁ¿£©¡£ÀýÈ磬ÏÂÁдúÂëÆ¬¶ÏÁ½´ÎÊä³ö¡®My name is Bob¡¯£º

<?php
$foo
= 'Bob';              // Assign the value 'Bob' to $foo
$bar = &$foo;              // Reference $foo via $bar.
$bar = "My name is $bar"// Alter $bar...
echo $bar;
echo
$foo;                // $foo is altered too.
?>

ÐèҪעÒâµÄÊÇÖ»ÓÐÃüÃû±äÁ¿²Å¿ÉÒÔ´«µØÖ·¸³Öµ£¬ÕâÒ»µã·Ç³£ÖØÒª¡£

<?php
$foo
= 25;
$bar = &$foo;      // This is a valid assignment.
$bar = &(24 * 7);  // Invalid; references an unnamed expression.

function test()
{
   return
25;
}

$bar = &test();    // Invalid.
?>




add a note add a note User Contributed Notes
±äÁ¿
stlawson AT sbcglobal DOT net
06-Jul-2002 09:47
In 'ghent's comment on the 'above example' I think he confuses the confusion ;)

Here is the example referred to:

<?php
$foo = 'Bob';              // Assign the value 'Bob' to $foo
$bar = &$foo;              // Reference $foo via $bar.
$bar = "My name is $bar";  // Alter $bar...
echo $bar;
echo $foo;                 // $foo is altered too.
?>

‘Reference $foo via $bar’/‘Alter $bar…’ IS correct, but it is a little obscure.  Here’s what it means:

$bar is assigned a reference to $foo, thus $bar ‘references’ or points to $foo which contains the string ‘Bob’.  Essentially what is happening here is ‘Bob’ is stored in memory at a particular address.  &$foo returns that address [where ‘Bob’ is stored].  That address is assigned to $bar.  So, in the string “My name is $bar”, $bar ‘uses’ the address it contains to find ‘Bob’ (which is ‘in’ $foo) and thus the string becomes “My name is Bob”.  When the string is assigned to $bar, because $bar refers to $foo, it gets assigned to the same address location that ‘Bob’ is stored at, thus ‘Bob’ is overwritten by “My name is Bob”.  The trick here is to realize that $bar behaves as if it is $foo, so when something is assigned to $bar (the alias of $foo), it’s as if it was being assigned to $foo!

After the above script is run, the output will look like this:

 My name is BobMy name is Bob

e.g. both $foo and $bar print the same thing.

In ‘C++’ the same code snippit would look like this:

foo = “Bob” ;
bar = &foo ;
bar = “My name is “ + *bar ;

Notice that in C/C++ it is necessary to manually dereference the pointer (*bar) – PHP does this automagically.

BTW: You might think that ‘echo $bar;’ would display the address of $foo – not so!  More PHP automagic ;)

<ÀàÐÍÏ··¨Ô¤¶¨Òå±äÁ¿>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22