PHP Intro

“What does PHP stand for?” Well, bulimics it doesn’t stand for words that begin with the letters P, H, and P, in that order, anymore. PHP was originally created from a set of scripts in the Perl programming language so that its inventor could publish web pages to his site more easily. He called it PHP for “Personal Home Pages”. As it grew to a more robust language, now based on C instead of Perl, the name was updated to sound more serious. It now “stands for” Hypertext Preprocessor. That’s a pretty good name, because that’s PHP’s  primary use — to run on a server and process information, then output Hypertext, or as it’s more commonly called, HTML (and CSS and Javascript and anything else that a browser can read). The preprocessing part is important – PHP is interpreted on a web server before the page is delivered to the browser. It is, therefore, considered a server-side language.

How to run PHP

Creating and running a PHP page on a web server is very similar to the way we created and uploaded HTML. PHP is a script that needs to be executed, though, so it must exist on a server to work — you can’t just drag it from your desktop into your browser the way you can with an HTML page. Just like HTML, though, it’s a text file that you upload via FTP and then visit with a browser.

PHP is often used within HTML, so that one file will have chunks of PHP and chunks of HTML. (In larger applications, developers usually organize their code so that the HTML is separate from the PHP, but for our purposes and scale, it’s fine to mix them.)

A few things to note in the example above:

  • The <?php indicates the start of PHP code and the ?> denotes the end.
  • Take note of the different comment styles. PHP uses // and /* */ much like Processing does. HTML uses <!–  –>
  • Variables in PHP begin with a dollar sign ( $ )
  • echo is the print function.
  • Statements end with a semicolon ( )
  • When you save the file, name it with the  .php extension, not .html 

PHP Language Basics

PHP syntax is similar to the syntax you use in Processing, but there are some important differences. Much of the programming concepts below you’ve learned already in ICM, but use this to review or understand them further.


One big difference from Processing syntax, is that variables in PHP always start with a dollar sign ($).

Variables in PHP are not strictly typed, meaning that you do not have to differentiate between strings, integers and so on. You don’t have to declare that a variable will hold a string or an integer, etc. before you use it. You can store a string on one line and an integer on another — PHP will handle that for you.

$myString = "hello world
echo $myString;
$myInteger = 1003;
echo $myInteger . "
$somethingelse = false;
echo $somethingelse . "

More Information

Mathematical Operations

$aValue = 0;
echo("aValue now = " . $aValue . "
$aValue = $aValue + $aValue;
echo("aValue now = " . $aValue . "
// % + - * / ++ -- and so on, same as in Processing/Java

More Information:

Control Structures, Logical Operators and Loops


// if Statement
$aValue = 0;
if ($aValue == 0){
echo("aValue is 0");
else if ($aValue == 1){
echo("aValue is 1");
else if ($aValue &gt;= 2){
echo 'aValue is greater than or equal to 2';
} else {
echo 'aValue something else';
// Other Logical Operators:  ==, <, >, <=, >=, ||, &&

// For loop
for ($i = 0; $i &lt; 10; $i++){
echo "$i = $i

// There are also while loops in PHP, just like in other languages. Look them up at if you're interested.

More Information:

Arrays and Loops

// Pretty normal array
$anArray = array();
$anArray[0] = "Something";
$anArray[1] = "Something Else";
for ($i = 0; $i &lt; sizeof($anArray); $i++)
echo $anArray[$i] . "

// Key Value Map
$anotherA = array("somekey" =&gt; "somevalue", "someotherkey" =&gt; "someothervalue");
$keys = array_keys($anotherA);
$values = array_values($anotherA);
for ($i = 0; $i &lt; sizeof($keys); $i++)
echo $keys[$i] . " = " . $values[$i] . "

More Information:



function myFunction($somearg)
// You would do something here
return "You passed in $somearg";

$passing_in = "Hello World";
$return_value = myFunction($passing_in);


More Information:

Built-in Functions

In addition to functions you write yourself, PHP has tons of functions already written for you. Function Reference

Getting user input

There are several ways of getting information from the user. Get, Post, Cookie, and Session are the ones we’ll look at.

The query string

The simplest way to pass variables from the browser into a PHP script in the URL. Any URL can have an optional query string, which looks like this:

The query string begins at the question mark (?). It can contain any number of variable and value pairs separated by an ampersand. The example above contains two variables, id and name. The values of those variables are 2 and Beth. And there’s an equals sign between the variable and the value.

Using $_GET

To get that information back out from PHP, we use a built-in variable called $_GET, which is an array. The values inside $_GET correspond to the information in the query string.

To get the values from the query string in the example URL above:

$user_id = $_GET['id'];
$user_name = $_GET['name'];

In this case, we’ve assigned the variables from the query string into variables in PHP. You can name your PHP variables anything you want. Inside the brackets of $_GET, however, the variable names must match the names in the query string.

So if the query string looked like:


then in order to pull the name “Beth” into PHP, the $_GET array would be:


Putting it all together …

$name = $_GET['firstname'];
echo "Hello, $name";


Building the front end of a form just requires that you use a specific set of HTML tags. Processing the information from the form is where PHP comes in. To learn about the available tags for creating HTML forms, there’s a great Mozilla Developer Network guide for that.
Where and how to send it
In the basic HTML form tag, there’s an action and a method.

<form action="processform.php" method="post">

The action tells where to send the form. The method defines the HTTP method used to send the form. Basically, the choices here are GET and POST. We looked at GET above — it takes the data and puts it in the URL query string. POST works in a very similar way, but instead of putting the data in the URL, it sends it in the HTTP header. POST is often preferable in a form because you don’t usually want all the form data displayed in the URL.

Using $_POST

$_POST works exactly like $_GET. It’s a built-in array with key-value pairs. When you submit a form using the POST method, use $_POST in PHP to get the data. The names of the keys in the $_POST array correspond to the names of the HTML form elements. The data from this form …

First name:
Last name:

… will be available as keys in PHP like this …

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>