Skip to content

Instantly share code, notes, and snippets.

@stungeye
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save stungeye/6b8f628660ad64e310e0 to your computer and use it in GitHub Desktop.

Select an option

Save stungeye/6b8f628660ad64e310e0 to your computer and use it in GitHub Desktop.
<?php require('animal_functions.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>Strange Animals</title>
</head>
<body>
<h1>Strange Animals</h1>
<?php if (selected_animal_and_count_are_valid()): ?>
<?php for ($i = 0; $i < selected_count(); $i++): ?>
<img src="<?= animal_filename() ?>" alt="<?= selected_animal() ?>">
<?php endfor ?>
<?php else: ?>
<p>Selected Count: <?= selected_count() ?> &mdash; Selected Animal: <?= selected_animal() ?> </p>
<p>The count must be a postive number no greater than <?= MAX_ANIMAL_COUNT ?>.</p>
<p>The animal must be one of the following: </p>
<ul>
<?php foreach(allowed_animals() as $animal): ?>
<li><?= $animal ?></li>
<?php endforeach ?>
</ul>
<?php endif ?>
</body>
</html>
<?php
const MAX_ANIMAL_COUNT = 20;
function allowed_animals() {
return [
'ayeaye',
'blueseaslug',
'blobfish'
];
}
function selected_animal() {
return isset($_GET['animal']) ? $_GET['animal'] : 'None'; // Defaults to None if not set.
}
function selected_count() {
return isset($_GET['count']) ? $_GET['count'] : 0; // Defaults to 0 if not set.
}
function selected_animal_and_count_are_valid() {
return (selected_count() > 0) // Count must be a positive number.
&& (selected_count() <= MAX_ANIMAL_COUNT) // Count must be no greater than MAX_ANIMAL_COUNT
&& in_array(selected_animal(), allowed_animals()); // Animal must be in our list of allowed animals.
}
function animal_filename() {
return selected_animal() . ".jpg"; // These files are assumed to be present in the current folder.
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment