Created
October 28, 2020 18:30
-
-
Save LGitHub-sprout/e79478cf3ffb78880b2c44b919673acc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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