PHP Example Code
Created Thursday 23 May 2019
Escaping special characters:
<html> <head> <title>Escapes</title> </head> <body> <?php echo "PHP is totally \"fun\""; echo "<br>"; echo "Gotta make that \$"; echo "<br><br>"; echo "\t Tabby-tab-tab"; echo "<br>"; echo "More \n new \n line \n fun. Maybe."; ?>
Math Operations:
<html> <head> <title>Mathops with Variables</title> </head> <body> <?php $x = 20; $y = 3; $add = $x + $y; $sub = $x - $y; $mult = $x * $y; $div = $x / $y; $mod = $x % $y; echo "Stuff we can do with VARIABLES. <br>"; echo "x is $x, y is $y<br>"; echo "add ".$add; echo "<br>"; echo "subtract ".$sub; echo "<br>"; echo "multiply ".$mult; echo "<br>"; echo "divide ".$div; echo "<br>"; echo "modulus ".$mod; echo "<br>"; ?> </body> </html>
IF
<?php
$value = 14;
if ($value < 12 )
	{ 
		echo "the value is LESS than 12";
	}
else 
	{	
		echo "it's MORE than or equal to 12";
}
echo "<br>and we're done";
?>
IF with ELSE
<?php
 //Notice the lack of { } for one-line blocks of if/else code.
 $age = 2;
 $playground_min_age = 5;
 $playground_max_age = 12;
 if ($age < $playground_min_age)
  echo "You are too young to play on the playground.  Go away!";
 elseif ($age > $playground_max_age)
   echo "You are too old to play on the playground.  Weirdo.";
 else
   echo "You can come play on the playground";
?>
FOR
<?php
 $count = 15;
  for ($i = 0; $i < $count; $i++)
  {
    echo "FOR STYLE! We are on $i in this loop!<br>";
  }
?>
WHILE
<html>
<title>While</title>
</head>
<body>
<?php
  $count = 0;
  $max= 15;
  echo "<ul>";
  while ($count < $max)
  {
	 echo "<li>We are on count number $count through this loop!</li>";
	 $count++;
  }
  echo "</ul>";
?>
</body>
</html>
Arrays:
<html>
<head>
<title>Arrays</title>
</head>
<body>
<?php
  //This creates an array and puts some initial values into it
  $myColors = array("red", "green", "blue");
 //Adds an additional element to the array (note the brackets)
 $myColors[] = "yellow";
 //Refer to an element in an array
 echo "The 0th color is ";
 echo $myColors[0];
 echo "<br>";
	
 echo "The (not really) 3rd element is ";
 echo $myColors[3];
?>
Associative Arrays
<html>
<head>
<title>Associative Arrays</title>
</head>
<body>
<?php
  //My associative array
  $marvel = array(   "Iron Man" => "Earth",
		      "Thor" => "Asgard",		
			"Hulk" => "Earth",
			 "Loki" => "Asgard",
			 "Captain America" => "Earth",
			"Hawkeye" => "Earth");
foreach($marvel as $character => $homeplace)
{
  if ($homeplace == "Earth"){
	  echo "$character is human";
  }
  elseif ($homeplace == "Asgard"){
	  echo "$character is alien or something";
  }
  else {
	  echo "I dont even know, man";
  }
  echo "<br>";
};
Nested Arrays (an Array of Arrays)
<title>Nested arrays</title>
</head>
<body>
<?php
//Notice below how I will use the ship's name for the key, and the ship's attributes for the sub-array items.
$ships = array();
                 
$ships["Nebuchadnezzar"] = array("Captain" => "Morpheus" , "First Mate" => "Trinity");
$ships["Osiris"] = array("Captain" => "Thadeus" , "First Mate" => "Jue");
$ships["Logos"] = array("Captain" => "Niobe" , "First Mate" => "Ghost");
$ships["Caduceus"] = array("Captain" => "Ballard" , "First Mate" => "Malachi");
//Let us print a HTML table out of this:
echo "<table>";
echo "<tr>";
echo "<th>Ship name</th>";
echo "<th>Captain</th>";
echo "<th>First Mate</th>";
echo "</tr>";
foreach($ships as $currShip => $currShipAtts)
{
  echo "<tr>";
  echo "<td>$currShip</td>";
  echo "<td> {$currShipAtts["Captain"]} </td>";
  echo "<td> {$currShipAtts["First Mate"]} </td>";
  echo "</tr>";
}
echo "</table>";
?>
Backlinks: FSU Courses:LIS5367:Class 2 Notes FSU Courses:LIS5367:Project Roadmap and Resources FSU Courses:LIS5367:olderfrontpage