<?php function doStuff($s) { return "."; } // Dont change below here // ============================================= assert(doStuff(1) == ".\n.", "Nope! Should be \".\n.\""); echo "========================================="; assert(doStuff(3) == "...\n.\n.\n.", "Nope! Should be \"...\n.\n.\n.\""); echo "========================================="; assert(doStuff(5) == ".....\n.\n.\n.\n.\n.", "Nope! Should be \"...\n.\n.\n.\n.\n.\""); // =============================================
<?php function doStuff() { // do stuff echo '<p>Message</p>'; } doStuff(); function doOtherStuff() { return 'Message'; } echo '<p>' . doOtherStuff() . '</p>'; function doMoreStuff($message = 'default message') { return '<p>' . $message . '</p>'; } echo '<p>' . doMoreStuff() . '</p>'; echo '<p>' . doMoreStuff('This space for rent.') . '</p>'; function foo($a, $b = 0) { $c = $a * $b; // $c is inside function scope return $c; } $c = 10; // global scope echo '<p>' . $c . '</p>'; echo '<p>' . foo(10, 5) . '</p>'; echo '<p>' . $c . '</p>'; // $c is unchanged
<?php function doStuff($message) { return '<p>' . $message . '</p>'; } echo doStuff('This is a message.');
<?php /* * * Pollute the heap. Helps trigger bug. Sometimes not needed. * */ class A { function __construct() { $a = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa'; $this->a = $a . $a . $a . $a . $a . $a; } } function doStuff($limit) { $a = new A(); $b = array(); for ($i = 0; $i < $limit; $i++) { $b[$i] = clone $a; } unset($a); gc_collect_cycles(); } $iterations = 3; doStuff($iterations); doStuff($iterations); gc_collect_cycles(); print_r(exif_read_data(__DIR__ . '/bug68799.jpg'));
<?php // assign default values to parameters (making them optional) // inside the parameter block; they become optional params. // note that optional parameters MUST come after required params function doStuff($message, $element = 'p') { return '<' . $element . '>' . $message . '</' . $element . '>'; } echo doStuff('This space for rent'); // a good practice is to pass in an associative array // containing all the function parameters when there are // many parameters a function may require. function doStuffBetter($options = array()) { // define default parameters inside function if (empty($options['message'])) { $options['message'] = 'This space for rent.'; } $options['element'] = empty($options['element']) ? 'p' : $options['element']; return '<' . $options['element'] . '>' . $options['message'] . '</' . $options['element'] . '>'; } echo doStuffBetter(array('element' => 'h2', 'message' => 'Another sample message.')); echo doStuffBetter(array('element' => 'h2'));
<?php function doStuff($x, $y, $z) { return null; } // Dont change below here // ============================================= assert(doStuff(1, 2, 3) == 6, "1) Nope!"); assert(doStuff(1000, 2, 3) == 6000, "2) Nope!"); assert(doStuff(59, 4, 89) == 21004, "3) Nope!"); // =============================================
<?php $string = 'dsfhlksdnhdnshsdhs'; function doStuff(&$string) { $string = 'NOT THE ORIGINAL'; } doStuff($string); echo $string, "\n"; $class = new stdClass(); $class->prop = 'value'; function doStuffOnObject($class) { $class->prop = 'newVal'; } doStuffOnObject($class); echo $class->prop, "\n"; $items = array(array('id' => 1), array('id' => 2)); foreach ($items as &$item) { $item['new'] = 'sdnfhkdsnlhs'; } var_export($items);