Introduction to PHP

Intro to PHP

Madeleine Guy
Fall 2014

What is PHP?

PHP Hypertext Processor "is a server-side scripting language designed for web development but also used a general-purpose programming language. As of January 2013, PHP was installed on more than 240 million websites." Wikipedia

It is best to know some of HTML and CSS to get the fullest advantage of PHP: with two or more programming languages, you can "generate dynamic page content; create, open, read, write, delete, and close files on the server; collect form data; send and receive cookies; add, delete, and modify data in a database; restrict users to access some pages on your website; and encrypt data" (w3 source below).

For more information, please feel free to read the great w3 'PHP 5 Introduction' page.

In short, PHP is a simple yet powerful tool that all information professionals should avail themselves of, even if they aren't programmers or in IT.

PHP tells a web server to look for executable code. Many programming languages, like C++ are compiled; however, the web server compiles PHP for you! As a PHP file loads, HTML is sent to the browser, and PHP is turned into a binary executable code. If the user wants to see your source, they will only see the HTML, because PHP runs on the server. This also allows you to do things normally not safe on Javascript or HTML with PHP.


Getting Started

This tutorial is for Mac OS users. Open Terminal (or perform a spotlight search and find Terminal).

To test your websites out on a real browser, it's best to log in to a server. All iSchool students have accounts on the iSchool server, so you can at least save your files. To log in, type in

     Image:php_login.png

You will be prompted for a password, which is the same as your eid password.

Once you're logged in, you'll see something like this:

     Image:php_fiat.png

To start playing with PHP, type

     Image:php_new.png


Now you've begun a new document, which can become anything from a website to a simple little form. First, type the beginning HTML tags:

     Image:php_nano.png

This is where some knowledge of HTML comes in handy. I'd recommend checking the iSchool's Introduction to HTML tutorial first if you've never encountered it before.

In short, we've begun an HTML document and opened the body, in which we'll put some of our php. After your closing header one tag, you should type:

     Image:php_php.png


This begins the PHP script. Please note that PHP scripts begin with "<?php" NOT "<?"

Now type the following on a new line:

     Image:php_hello.png

Echo is a command which tells the browser to output whatever you tell it. For now, it doesn't seem that impressive since all you wrote is some text. However, with more advanced PHP, you can use echo statements in conjunction with multiple programming languages, producing powerful results.

Note that text within echo statements must be nested within HTML tags - in this case, we used the bold tags.

The \n part of our statement is PHP code for "new line" - somewhat easier than having to remember to type the HTML code for new line (< br />) over and over again.

Lastly, please note that all PHP commands must end in a semicolon. Without that semicolon ending, you will probably get a bunch of error messages, and your code will not work!

Next type:

     Image:php_echo.png

This time we have nested our text within the HTML tags for paragraph.

Lastly, let's close out our PHP script:

     Image:php_close.png

Then, let's close the HTML document:

     Image:php_end.png


If you've typed everything right, you should be able to test your PHP at the following links:

Link 1
Link 2

For these websites, you should only paste the echo statements and (if the option is given) choose the 'Display as HTML' option.

The echo statements, for your reference (remove the spaces in the HTML brackets):

echo "< b>Hello, world!</ b>\n";
echo "< p>This is my first PHP script!</ p>";


If you were able to save your file onto a server, you should get a result like this:

     Image:php_site.png