΢ Ͷ ^ ھ

cba赛程广东 :PHP׃ȫ׃&ֲ׃&global&staticPI÷

cba㶫 www.axwwg.com  •rg2020-01-24 13:00:34   ߣ   ҪuՓ(0)

ČvPHP׃ȫ׃&ֲ׃&global&staticPI÷oҹ҅w£
҂֪׃䌍ஔ҂Á탦Ϣ

ČvPHP׃ȫ׃&ֲ׃&global&staticPI÷oҹ҅w£

҂֪׃䌍ஔ҂Á탦ϢPҎtʲôŴҲDZ^@Ͳ٘@Ҫ́PHP׃ăɂȫ׃&ֲ׃

@ɂ׃ŴXFɂ~global&static]e@ɂ~

҂֪кⲿx׃ȫ˺ȫ׃Ա_еκβLҪһLһȫ׃Ҫʹ global PIȻ PHP Ȳ•׃Ǿֲ׃HںȲL҂

<?php
$x=5; // ȫ׃
function myTest()
{
  $y=10; // ֲ׃
  echo "<p>yԇ׃:<p>";
  echo "׃ x : $x";
  echo "<br>";
  echo "׃ y : $y";
}
myTest();
echo "<p>yԇ׃:<p>";
echo "׃ x : $x";
echo "<br>";
echo "׃ y : $y";
?>

ό myTest() x $x $y ׃$x ׃ں•ȫ׃ $y ׃ں•Ǿֲ׃

҂{myTest()ݔɂ׃ֵ,ݔֲ׃ $y ֵDzݔ $x ֵ $x ׃ںⶨxoںʹҪһLһȫ׃Ҫʹ global PI

Ȼ҂myTest()ݔɂ׃ֵݔȫֲ׃ $x ֵDzݔ $y ֵ $y ׃ںжxھֲ׃

ɴ҂Ϳ֪҂ڲͬʹͬ׃Q@Щȶx׃Ǿֲ׃ֻԓ@ҲһС˹

UԒf҂ȁglobalPIglobal PIںLȫ׃ں{úⶨxȫ׃҂Ҫںе׃ǰ global PI

<?php
$x=5;
$y=10;
function myTest()
{
  global $x,$y;
  $y=$x+$y;
}
myTest();
echo $y; // ݔ 15
?>

PHP ȫ׃惦һ $GLOBALS[index] ĔM index ׃Q@MںȲLҲֱÁȫ׃ČԌ@ӣ

<?php
$x=5;
$y=10;
function myTest()
{
  $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];
}
myTest();
echo $y;
?>

˽˰ð҂فstaticPI

һɕr׃ͨhȻЕr҂ϣijֲ׃ҪhҪ@һcҪ҂һ•׃rʹ static PI҂

<?php
function myTest()
{
  static $x=0;
  echo $x;
  $x++;
}
myTest();
myTest();
myTest();//\нY 0 1 2
?>

҂Կÿ{ԓrԓ׃ǰһα{Õrֵ^@ۂҪ֪ԓ׃ȻǺľֲ׃لeĵطʹ^҂ʹfwĕr߀Ǖõ@ʲôstaticPIֵdȤо?@~?

ͨ^ĸNcŽׂY

1xںⲿľȫ׃Ķx̎һֱļYβ
2ȶx׃Ǿֲ׃麯x
3֮g򻥲Ӱ
4Lȫ׃Ҫ global PIֻʹ $GLOBALS[index] M

php кЪԾֲ׃wȫ׃ʹֲ׃вx]ȫ׃ͬ׃Ҳwʾ

<?php
$a=5;
$b=3;
function t()
{
  echo $a-$b; // ݔ 0
}
t();
?>

Ҫںֱʹȫ׃ͨ^ global PI•ͨ^ php Զx $GLOBALS M@??/p>

<?php
$a=5;
$b=3;
function t1()
{ 
  global $a,$b;
  echo $a-$b; // ݔ 2
}
t1();
echo PHP_EOL;
function t2()
{
  echo $GLOBALS['a']-$GLOBALS['b']; // ݔ 2
}
t2();
?>

ξ͵@

PPHPPݸdȤx߿ɲ鿴վ}phpúcɿYphpַ(string)÷YPHPM(Array)ɴȫPHPYc㷨̡̳phpOӋ㷨Y

ϣPHPOӋ

ܸdȤ:

  • php׃
  • \Մphpincludeļ׃
  • \ՄPHP׃ԼַÆ}
  • \php׃һЩ}
  • PHP staticֲoB׃ȫoB׃Y
  • PHP globalȫ׃䑪cע헷$GLOBALS÷ȡ
  • PHPAxȫ֔M׃СY
  • ȫ˽PHPеȫ׃
  • PHPAx׃9ȫ֔M÷Ԕ
  • phpglobal$GLOBALS[]ķ֮һ
  • PHP Globalxȫ׃ʹf
  • phpstaticoB׃ʹ÷Ԕ

P

  • PHP׃ȫ׃&ֲ׃&global&staticPI÷

    PHP׃ȫ׃&ֲ׃&global&staticPI÷

    ČvPHP׃ȫ׃&ֲ׃&global&staticPI÷oҹ҅w£ ҂֪׃䌍ஔ҂Á탦Ϣ
    2020-01-24
  • thinkphp{sqlserver^̷ضY

    thinkphp{sqlserver^̷ضY

    ȰbUչ windows ֞ɂE ҵԼPHP汾pdoUչd≺php.ini憢ÔUչ,ҪעĆ}php汾ԼǷ鰲ȫ汾
    2020-01-24
  • Laravel·cMVCԔ

    Laravel·cMVCԔ

    ČvLaravel·cMVCoҹ҅w£ 1· ·ɵþnjÑIJͬurlՈDloijM̎laravel·ɶx
    2020-01-24
  • Laravel5.1 ܵ䛺עԌFԔ

    Laravel5.1 ܵ䛺עԌFԔ

    ČvLaravel5.1 ܵ䛺עԌFoҹ҅w£ Pڵ䛺ע LaravelԎһ׽MF@һ҂ֻҪFε
    2020-01-24
  • PHP߼֮ϢԭcFԔ

    PHP߼֮ϢԭcFԔ

    ČvPHP߼֮ϢԭcFoҹ҅w£ 1. ʲôϢ ϢУӢZMessage queueһNMgͨŻͬ
    2020-01-24
  • phpstormME̳Ԕ

    phpstormME̳Ԕ

    PhpstormǴPHPT۲ֵһaļ_l֧PHPZԹ ṩĴaaȫؘre`AȵȹnjһЩphp
    2020-01-24
  • Yii Fܺͽܵʾa

    Yii Fܺͽܵʾa

    YiiֺܶĔ҂܉Mд惦չʾ,ܴaһЩ^ҪļϢ@r҂Ҫ҂@ЩϢMм һܴaMмܺC͑
    2020-01-24
  • php@ȡܕrgڸʽČa

    php@ȡܕrgڸʽČa

    rgʽ //@ȡ_ʼrgͽYrg $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y')); $endToday=mktime(0,0,0,date('m'),date('d')+
    2020-01-24
  • PHPWļɺ͌WҪؿY

    PHPWļɺ͌WҪؿY

    1mPHPWߵČW· (1) ˽HTML/CSS/JSW퓸ԪꮅAο캆ԵľWԪc˽ (2) ˽ӑBZĸ\C˽
    2020-01-24
  • PHPهע֪Rc\

    PHPهע֪Rc\

    هע һõĴaYOӋһϵ@ҲǺܶͨOӋģʽּǰѷɢڸ̎ͬһܵĴaR۵һγһ??Ȼ
    2020-01-24