scope.php

<?php
/* Declare global variable, which is factor by which to increase argument
 */
$SCALE_FACTOR = 5;

/* This function returns scale factor times its argument, but leaves
 * original value unchanged since call is by value.
 */
function scaled( $x ) {
  global $SCALE_FACTOR; // access the factor
  $x *= $SCALE_FACTOR;
  return $x;
}

/* This function returns no value.  It uses call by reference
 * to scale its argument.
 */
function scale( &$x ) {
  global $SCALE_FACTOR; // access the factor
  $x *= $SCALE_FACTOR;
}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Scope example</title>
    <meta charset="utf-8">
  </head>
  <body>
    <?php
    print "scale factor = $SCALE_FACTOR<br />"; // global variable is visible in different php block
    $x = 3;				// original value of x
    print "x = $x<br />";		// print it to see
    print "$SCALE_FACTOR*x=" . scaled($x) . "<br />"; // print scaled x but leave x unchanged
    print "x = $x<br />";		 // show that x is unchanged
    scale($x);				 // now scale x for real
    print "x = $x<br />";		 // show that x is scaled
    ?>
  </body>
</html>