* @return array * * array( * 'kat1baked', * 'kat2baked', * 'kat3baked' * ) * */ function makeCookies($num, $name, $shouldBake = true) { $cookies = []; $cookies = array(); // This is a "ternary" operator $bakedString = $shouldBake == true ? 'baked' : 'not baked'; // This is the same as line 23 if ($shouldBake == true) { $bakedString = 'baked'; } else { $bakedString = 'not baked'; } for ($i = 1; $i <= $num; $i++) { $cookies[] = $name . ' ' . $i . ' ' . $bakedString; } return $cookies; } // Function usage $result = makeCookies(5, 'kat', false); echo '<pre>'; print_r($result); echo '</pre>';
* @param string $name Name to print on each cookie * @param bool $shouldBake Should we bake these? By default we will bake these! * * @return array */ function makeCookies($howMany, $name, $shouldBake = true) { $cookies = array(); // if ($shouldBake == true) { // $cookieName = $name . ' Baked Cookie'; // } else { // $cookieName = $name . ' Cookie Dough'; // } // You can also rewrite the conditional above using a ternary, shortening 5 lines of code into 1 $cookieName = $shouldBake ? $name . ' Baked Cookie' : $name . ' Cookie Dough'; // Loop over the $howMany variable, to create elements in the $cookies array for ($i = 1; $i <= $howMany; $i++) { // array_push($cookies, $cookieName . ' - ' . $i); $cookies[] = $cookieName . ' - ' . $i; } // Return the array we just created, like we promised we would do in the DocBlock return $cookies; } echo '<pre>'; // Make some baked cookies $cookies = makeCookies(5, 'Birthday'); //print_r($cookies); //die(); // I'll take the cookie dough! $cookieDoughCookies = makeCookies(12, 'Bar-Kie', $shouldBake = false); print_r($cookieDoughCookies);