<?php //all constants will be global //in fact, PHP wont allow you to declare a constant inside a function const ALL_CONSTANTS_ARE_GLOBAL = "Since a constant cant be changed, it has global visibility"; echo "<p>Accessing a constant at the global level: " . ALL_CONSTANTS_ARE_GLOBAL . "</p>"; //create and output a variable outside a function $declaredGlobally = "This was declared globally!"; echo "<p>The \$declaredGlobally variable begins with value: <strong>" . $declaredGlobally . "</strong></p>"; //-------------------------------------------------------------------------------- /* define a function what happens in the function is isolated from the code outside the function, and vice-versa */ function someFunction() { echo "<p>Accessing a constant from inside a function: " . ALL_CONSTANTS_ARE_GLOBAL . "</p>"; echo "<p>Try to access from inside a function a variable declared outside. This creates an error message. " . $declaredGlobally . "</p>"; $declaredInsideAfunction = "We're in las vegas, baby!"; echo "<p>A variable declared inside a function can be accessed inside that function: " . $declaredInsideAfunction . "</p>"; } someFunction(); echo "<p>A variable declared inside a function CANNOT be accessed outside that function: " . $declaredInsideAfunction . "</p>"; //test the value of the global variable again echo "<p>The \$declaredGlobally variable ends with value: <strong>" . $declaredGlobally . "</strong></p>"; //-------------------------------------------------------------------------------- ?> </span> </body> </html>
$sql = "SELECT * FROM trial WHERE id =17"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { echo $row['image']."<BR>"; } */ $memory2 = memory_get_peak_usage(true); function someFunction($start, $limit, $step = 1) { $result = ''; for ($i = $start; $i <= $limit; $i += $step) { (yield $i . " "); } //return $result; } foreach (someFunction(0, 10) as $value) { echo $value; } echo "<br>"; echo $memory2 . "<br>"; echo "<br><br>"; $memory = memory_get_peak_usage(true); function someFunction2($start, $limit, $step = 1) { $result = ''; for ($i = $start; $i <= $limit; $i += $step) { $result .= $i . " "; } return $result; } echo someFunction2(0, 10);
<?php // used in callback class X { function y() { } } // never used class X2 { function y() { } } // used but not in callback class X3 { function y() { } } // normal usage array_filter(array(1, 2, 3), array('X', 'Y')); // in a array structure, but not for callback someFunction(array(1, 2, 3), array('X3', 'Y'));
<?php use PHPFluent\EventManager\Manager; require_once 'bootstrap.php'; function someFunction(Manager $eventManager) { echo "I'm executing\n"; echo "Some operation\n"; $eventManager->dispatchEvent("updated"); } someFunction($eventManager); echo file_get_contents("sample.txt") . PHP_EOL; $eventManager->dispatchEvent("remove.file.sample.txt", array((new DateTime())->format("m/d/Y")));
<?php // Not a double assignation, since it is reprocessed $a = someFunction($b); $a = strtolower($a); // Double assignation, since it is reprocessed $a2 = someFunction($b2); $a2 = strtolower($b2);