search

Home  >  Q&A  >  body text

In the example of the PHP global keyword, the output is not equal to 15 but is actually equal to 10

The code is as follows

<?php

$x=5;

$y=10;

function myTest()

{

global $x,$y;

$y=$x $y;

}

myTest();

echo $y; // Output 15

?>


石岳石岳2376 days ago1242

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2018-07-25 21:43:33

    The answer is 10, that’s right. First of all, you have to understand the definition of the keyword global. There is a big difference between global and $GLOBALS. $GLOBALS defines variables as global variables. If it is defined by $GLOBALS, the result will be natural. It is 15, and global is used as a reference. Please note that the reference is not a pointer, and it cannot ultimately change the result of $x or $y!

    reply
    2
  • 范珺

    范珺2018-07-25 18:11:07

    It seems to be 15

    reply
    0
  • Cancelreply