Skip to content

Instantly share code, notes, and snippets.

@LGitHub-sprout
Created October 28, 2020 18:30
Show Gist options
  • Save LGitHub-sprout/e79478cf3ffb78880b2c44b919673acc to your computer and use it in GitHub Desktop.
Save LGitHub-sprout/e79478cf3ffb78880b2c44b919673acc to your computer and use it in GitHub Desktop.
<?php
/**
* Variable Scope Example #1:
*
* $temp = preceding text.
* notice DECLARED var name $temp and var passed into somedate() don't have to match
* but RETURNED var name and passed var must match
**/
$temp = "Ex. #1: The date is "; // set outside function
echo somedate( $temp, time() ); // but passed in as $arg
function somedate( $text, $timestamp ) { // passed, returned $var must match
return $text . date( "l F jS Y", $timestamp );
}
function somedate2() {
$temp2 = date( 'l F jS Y', time() ); // var set inside function is out of scope in non-function code.
echo "<h4>$temp2 set inside somedate() function will display</h4>";
return "<p>This is <code style='font-size: 15px;color:red;'>somedate()</code> as returned inside function: $temp2" . "</p>";
}
// $temp2 displays bc it's in scope (inside delared function)
echo somedate2();
// $temp2 won't display... WHY?
echo '<p>This is $temp2 var: ' . $temp2 . '</p>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment