PHP

PHP is a widely used server side web language.

w3schools PHP intro has a nice list of things PHP can do.

We will work toward using PHP to access a SQL database. It can do many other things, like generating custom HTML pages or retrieving data from other websites.

PHP Reading Material

Other than the PHP Intro, you should also visit these two pages at a minimum:

PHP Syntax

PHP Variables

PHP is a full programming language that can do all the usual stuff: loops, functions, arrays, math, string operations, etc.

Error Messages in PHP

If you create a PHP page and it doesn’t work, you might be confronted with a blank page in your browser. You’ll need some error information to solve your problem.

PHP is a little trickier to debug since it doesn’t automatically give error messages. One way to get that info is to add this line (within php tags) to your page before any other PHP commands:

error_reporting(E_ALL);

This will cause error messages to be displayed in your browser when you run the page. Typically, full error reporting is disabled by default to avoid giving potential hackers more information about the internal workings of the website.

Beyond seeing the error messages, you can also add code to echo the value of a variable to see if things are behaving as expected, and you can disable certain lines of code with commenting to help you locate problems.

Another way to see your PHP errors is to look in an error log file that is automatically generated in the server. In Cloud9, that is located in your root folder, one level up from your workspace folder. Here are some commands to open that file from your bash terminal:

cd ..                   //go up one folder level
ls                      //list files in folder
c9 php_errors.log       //open file in c9 editor

You can also click the gear icon in your Workspace pane (left side) and click “Show Home in Favorites.” The error log file can now be found under the root folder, labeled as “~-.” in the Workspace pane.

You should look into better debugging options if you ever find yourself working extensively in PHP.

Mad Lib / Form Handling

PHP Form Handling

To create a Mad Lib, you need two pages: an HTML form and a PHP page to process the data from the form.

The HTML form will ask the user for one or more random words.

When the user submits the form, it sends the words to the PHP page, and the PHP code generates text for the HTML page that inserts the words into sentences.

PHP Basics:

To make the server read your PHP code, you must use the “.php” extension. The server will not read PHP code from an “.html” file.

Enclose your PHP code in the following tags:
Opening tag: ” <?php ”
Closing tag:  ” ?> ”

Example: <?php echo “Hello World!”; ?>

You should start by trying to get that type of a line to work in a PHP file on your server space.

The “echo” command sends html code to the browser. You can include HTML tags inside the quotes to include formatting or to create HTML elements.

Example: <?php echo”<h1>This is a heading generated from PHP code.</h1>; ?>