search

Home  >  Q&A  >  body text

There is a problem with the self-study example code

PHP global keyword

global keyword is used to access global variables within a function.

To call a global variable defined outside a function within a function, we need to add global before the variable in the function Keywords:

Instance

<?php
$x=5;
$y=10;

fun ction myTest()
{
global $x,$y;
$y=$x $y;
}

myTest();
echo $y; // Output 15
?>



Output 15 here should be output 10, myTest() cannot output here, you should add an echo $y

phpcn_u2188phpcn_u21882874 days ago524

reply all(1)I'll reply

  • 黄舟

    黄舟2017-05-15 15:19:14

    Okay thanks for the feedback!

    reply
    0
  • Cancelreply