search

Home  >  Q&A  >  body text

Why $GLOBALS['y'] is 10

<?php
$x=10;
$y=20;
function test(){
global $x,$y; //Use global keyword

$y=$x $y;
}
test();
echo $y;


##<? php

$x=5;

$y=10;

function myTest()

{

$GLOBALS['y']=$GLOBALS['x'] $GLOBALS['y'];

}

myTest();

echo $y;

?>

Are these two writing methods the same? Why is the returned result one 30 global $y

牧云软件园牧云软件园2221 days ago1282

reply all(2)I'll reply

  • Summer

    Summer2018-11-14 18:15:43

    Only the first $y is a global variable

    reply
    0
  • 牧云软件园

    It seems that external global variables cannot be changed inside a function.

    牧云软件园 · 2018-11-19 09:14:11
  • Cancelreply