Skip to content

Instantly share code, notes, and snippets.

@griffiths
Created April 2, 2012 17:06
Show Gist options
  • Save griffiths/2285175 to your computer and use it in GitHub Desktop.
Save griffiths/2285175 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>*** PHP ***</title>
</head>
<body>
<?php
// This is a comment
// Print a string of text on our page
echo "<div>Some text</div>";
// Storing names a series of variables
$name1 = "Kelly";
$name2 = "Kate";
$name3 = "Susan";
// Storing numbers as variables
$apples = 20;
$oranges = 35;
// Simple math using two variables
$fruit = $apples + $oranges;
// Printing the resulting value (apples + oranges)
echo "<div>" . $fruit . "</div>";
// ** ARRAYS **
// Storing a series of names in an array
$names = array("Susan", "David", "Kate", "Emmett", "Mary", "Frank", "Elisabeth", "Alex", "Kyle", "Sarah");
// Prints the number of names (values) in the array
echo "<div>" . count($names) . "</div>";
// Prints every value in the names array
// print_r($names);
// Printing specific values from our array
echo $names[0] . ", " . $names[1] . ", and " . $names[2] . " went to the market.";
// ** LOOPS **
// Opening a div to contain our loop
echo "<div>";
// Setting our counter to 0
$countb = 0;
// Setting up our loop -- the code within the loop should execute for every name in our array
while ($countb < count($names)) {
// Print the name at position $countb
echo $names[$countb];
// Print a comma after each name, unless we are at the last position in the array
if ($countb < (count($names)-1)) {
if ($countb == 5) {
echo " and Tom";
}
echo ", ";
} else {
// Print a period if we are at the last position in the array
echo ".";
}
if ($countb == (count($names)-2)) {
// If we are at the second to last position in the array, print "and "
echo "and ";
}
// Add one to our counter
$countb = $countb + 1;
}
// Closing our containing div
echo "</div>";
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment