Coding

PHP Sandbox

A sandbox is somewhere were you can play. That's what a PHP sandbox is, as well, a place where you can put in some PHP code snippets and see if they are going to work. This is a great place for you to experiment with commands to see how they actually work. While you can find many of them using a Search Engine, you can also follow this link to find one here at PHP Sandbox.

As an example follow that link and replace the code with the following:

for ($loop = 0; $loop < 20; $loop++) {
  $number = rand(5,10);
  echo $number."\n";
}

Execute the code. What do you think each of the different parts of this code snippet do? Try deleting or changing things to get an idea of how this works together. 

PHP Syntax

PHP is a scripting language.  It stands for "pre hypertext processing" which means it is used to generate output before hypertext is processed.  Think about it like this:  the PHP scripting is used to generate some information.  This information can be used to generate output which is added to an HTML document before the HTML document is displayed by your client.  All of the work is done at the server, so none of the information is shown to the user.  

PHP scripts (programs) are located inside php files, rather than html files.  PHP files can contain PHP and html, but html files can not contain PHP.  Sounds confusing, but it's not.  What this means is that, instead of a file called "index.html", you will have a file called "index.php".  Your index.php file can contain all of your existing html, but you can also add in sections that are PHP scripts.

For more information, visit: http://www.hungrybeagle.com/index.php/forum/it11-12-php/6-php-syntax

Things you should know how to do:

  • insert a PHP section into a *.php document
  • add comments to a PHP section
  • enter basic output commands to a PHP section

Assigning and Using Variables

Output in PHP would look no different from regular HTML if we couldn't make the content dynamic. Dynamic content means it can change based on certain conditions.  In PHP, we can use nicknames for things that can change.  Just like in math, these are called variables.

Variables are extremely important, because we can make decisions based on what the variables are equal to, or we can do math to store values into variables for later use, or we can just display the contents of a variable.

  • In PHP, variables always begin with a $ sign.  
  • Variable names are case sensitive
  • There are many types of variables. Today we will look at two kinds of variables: strings and values
  • Variables are assigned using a single "=" symbol

For more information about variables, visit the thread in the forums: http://www.hungrybeagle.com/index.php/forum/it11-12-php/7-php-variables

Things you should be able to do after today:

  • store a value in a variable
  • display the contents of a variable using the echo command
  • perform math calculations and store the contents into a variable
  • use variables to perform calculations

Login Form