PHP问题
THX~
ä¾å 1. ä½¿ç¨ globalå
³é®å
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
以ä¸èæ¬çè¾åºå°æ¯â3âãå¨å½æ°ä¸ç³æäºå ¨å±åé $a å $bï¼ä»»ä½åéçææå¼ç¨åéé½ä¼æåå°å ¨å±åéã对äºä¸ä¸ªå½æ°è½å¤ç³æçå ¨å±åéçæ大个æ°ï¼PHP 没æéå¶ã
å¨å ¨å±èå´å 访é®åéç第äºä¸ªåæ³ï¼æ¯ç¨ç¹æ®ç PHP èªå®ä¹ $GLOBALS æ°ç»ãåé¢çä¾åå¯ä»¥åæï¼
ä¾å 2. ä½¿ç¨ $GLOBALS æ¿ä»£ global:
<?php
$a = 1;
$b = 2;
function Sum()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Sum();
echo $b;
?>
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
以ä¸èæ¬çè¾åºå°æ¯â3âãå¨å½æ°ä¸ç³æäºå ¨å±åé $a å $bï¼ä»»ä½åéçææå¼ç¨åéé½ä¼æåå°å ¨å±åéã对äºä¸ä¸ªå½æ°è½å¤ç³æçå ¨å±åéçæ大个æ°ï¼PHP 没æéå¶ã
å¨å ¨å±èå´å 访é®åéç第äºä¸ªåæ³ï¼æ¯ç¨ç¹æ®ç PHP èªå®ä¹ $GLOBALS æ°ç»ãåé¢çä¾åå¯ä»¥åæï¼
ä¾å 2. ä½¿ç¨ $GLOBALS æ¿ä»£ global:
<?php
$a = 1;
$b = 2;
function Sum()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Sum();
echo $b;
?>
åèèµæï¼PHPæå
温馨提示:答案为网友推荐,仅供参考