function simple($n, $m = 2) { if ($m >= $n) { return 'просто'; } if ($n % $m == 0) { return 'не е просто'; } return simple($n, $m + 1); }
function yml($data, $pref = "units_", $useKey = false) { $s = ""; $i = 0; foreach ($data as $key => $l) { if (!$useKey) { $s .= trim($pref . "_" . simple($l["nom"])) . ":\n"; } else { $s .= trim($key) . ":\n"; } foreach ($l as $key => $value) { if (!empty($value) || $value === "0") { $s .= " " . enc($key) . ": " . enc($value) . "\n"; } } $i++; $s .= "\n"; } return $s; }
return $t['sec'] + $t['usec'] / 1000000; } function start_test() { ob_start(); return getmicrotime(); } function end_test($start, $name) { global $total; $end = getmicrotime(); ob_end_clean(); $total += $end - $start; $num = number_format($end - $start, 3); $pad = str_repeat(" ", 24 - strlen($name) - strlen($num)); echo $name . $pad . $num . "\n"; ob_start(); return getmicrotime(); } function total() { global $total; $pad = str_repeat("-", 24); echo $pad . "\n"; $num = number_format($total, 3); $pad = str_repeat(" ", 24 - strlen("Total") - strlen($num)); echo "Total" . $pad . $num . "\n"; } $t0 = $t = start_test(); simple(); $t = end_test($t, "simple");
include 'header_body.php'; echo "<h1>" . htmlspecialchars($title) . "</h1>\n"; echo "<p>" . $text . "</p>\n"; echo "</body></html>\n"; } $script = __DIR__ . $components[1] . '/index.php'; if (file_exists($script)) { include $script; } elseif (file_exists("{$_SERVER['DOCUMENT_ROOT']}/a/{$buildid}/js/site/page/{$components['1']}.js")) { simple('Not Available', "Sorry Javascript is required to view this page."); } else { $c = curl_init($apiURL . "/post{$path}"); curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE); $r = curl_exec($c); if ($e = curl_error($c)) { simple('502 API Not Available', $e, 502); return; } $j = json_decode($r); if (!$j->e) { $url = $path; $title = $j->title; include 'header.php'; echo "</head><body>\n"; include 'header_body.php'; echo $j->content; echo "\n</body></html>\n"; } else { simple('404 Not Found', "This page does not exist.", 400); } }
<?php ## Каждый генератор - это объект function simple($from = 0, $to = 100) { for ($i = $from; $i < $to; $i++) { echo "значение = {$i}<br />"; (yield $i); } } $generator = simple(); echo gettype($hello); // object
<?php ## Использование генератора без foreach function simple($from = 0, $to = 100) { for ($i = $from; $i < $to; $i++) { (yield $i); } } $obj = simple(1, 5); // Выполняем цикл, пока итератор не достигнет конца while ($obj->valid()) { echo $obj->current() * $obj->current() . " "; // К следующему элементу $obj->next(); }
<?php ## Создание генератора function simple($from = 0, $to = 100) { for ($i = $from; $i < $to; $i++) { (yield $i); } } foreach (simple(1, 5) as $val) { echo $val * $val . " "; } // 1 4 9 16
<?php ## Простейший генератор function simple($from = 0, $to = 100) { for ($i = $from; $i < $to; $i++) { echo "значение = {$i}<br />"; (yield $i); } } foreach (simple() as $val) { echo "квадрат = " . $val * $val . "<br />"; if ($val >= 5) { break; } }
pos_one: id: 1 civilisation_id: 1 evolution_id: 1 */ $posId = 1; $poss = array(); for ($civ = 2; $civ < count($data); $civ++) { $civ_name = $data[$civ][1]; $civ_id = $data[$civ][0]; if ($html) { echo "<h1>{$civ_name} ({$civ_id})</h1>"; } for ($unit = 2; $unit < count($data[0]); $unit++) { $unit_name = $data[1][$unit]; $unit_id = $data[0][$unit]; $ok = $data[$civ][$unit]; $color = $ok ? "blue" : "red"; if ($html) { echo "<p><span style=\"color:" . $color . "\">" . enc($unit_name) . " ({$unit_id})" . "</span></p>\n"; } if ($ok) { $poss[simple($civ_name) . "__" . simple($unit_name)] = array("id" => $posId, "civilisation_id" => $civ_id, "evolution_id" => $unit_id); $posId++; } } if ($html) { echo "<br><br>"; } } echo yml($poss, "pos", true);
</td> </tr> <tr> <th> Website: </th> <td> <?php echo empty($db_object->array['website']) ? 'None' : plain($db_object->array['website']); ?> </td> </tr> <tr> <td colspan="2" style="text-align: center;"> <?php echo empty($db_object->array['bio']) ? ' ' : simple($db_object->array['bio']); ?> </tr> </tr> </table> </div> <div id="gravatar_div" class="span-4 last"> <a href="http://en.gravatar.com/site/check/<?php echo $db_object->array['email']; ?> " target="_blank"> <img src="<?php echo BackendUser::getGravatar($db_object->array['email']); ?> " alt="Gravatar" /> </a>
function simple($n) { static $a = 2; static $b = true; if ($n == 2) { return $b; } if ($n > $a) { if ($n % $a == 0) { $b *= false; } $a++; simple($n); } return $b; }