Learn PHP Notes

<?php phpinfo(); ?>

Echo command

<?php echo "The echo command is the preferred way to write to the screen."; ?>
<?php print "The print command is similar to echo, but not recommended."; ?>
<?php echo "Randy" . " Russell uses a period for concatenating strings."; ?>
<?php echo 20+22; ?>
<?php echo "One can use <strong>HTML tags</strong> inside of PHP statements."; ?>

<?php
   // Single line comment example
   /* Multi-line comment example
   Just like Flash! */
echo "Make sure to comment your code."; ?>

<?php echo 'One can use single quotes or double quotes around strings in PHP.'; ?>

Variables

<?php
   $var1 = 42;
   echo $var1;
   echo "<br />";
   $var2 = "Hello Fly!";
   echo $var2;
   echo "<br />";
   echo $var2 . " Have a beer!<br />";
   echo "{$var2} Have some nachos!<br />";
?>

Arrays

<?php
   /* Create an array with 3 elements */
   $myarray = array('Fly', 'Cathy', 'Sean');

   echo "<p>Hello " . $myarray[0] . ", " . $myarray[1] . ", and " . $myarray[2] . "!</p>";

   /* Replace an item in the array at a specific index */
   $myarray[1] = 'Nuff';
   /* Add a new item to the end of the array */
   $myarray[3] = 'Babushka';
   /* Add a new item to the end of the array without needing to know array's length */
   $myarray[] = 'Henry';
   echo "<p>Howdy " . $myarray[0] . ", " . $myarray[1] . ", " . $myarray[2] . ", " . $myarray[3] . ", and " . $myarray[4] . "!</p>";

   /* Create an associative array */
   $moonsCount['Venus'] = 0;
   $moonsCount['Earth'] = 1;
   $moonsCount['Mars'] = 2;
   $moonsCount['Jupiter'] = 63;

   echo '<p>Mars has ' . $moonsCount['Mars'] . ' moons. Jupiter has ' . $moonsCount['Jupiter'] . ' moons.</p>';

   /* Another way to create an associative array */
   $mainGasAtmosphere = array('Earth' => 'nitrogen', 'Mars' => 'carbon dioxide', 'Jupiter' => 'hydrogen');

   echo '<p>The main gas in the atmosphere of Mars is ' . $mainGasAtmosphere['Mars'] . '. The main gas in the atmosphere of Jupiter is ' . $mainGasAtmosphere['Jupiter'] . '.</p>';
?>