Basic PHP Tutorial

The Unofficial PHP Tutorials

Intro to PHP, Hypertext Preprocessor

So someone wants to learn about PHP, the Hypertext Preprocessor. These tutorials aim to teach a beginner enough PHP to learn more advanced topics at their leisure.

When you are finished with these tutorials, you should be able to install a web server, write a PHP application, and host a basic web site.

PHP Preliminaries

A little bit of history is in order when learning about PHP, as it’s good to know the history of something when using it. This will be a brief intro to the history of PHP, the Hypertext Preprocessor.

PHP was first created in 1995 by a guy named “Rasmus Lerdorf”; at first, it was known as “Personal Home Page”. Written in the C Programming language, PHP possessed speed and power, yet was designed to have an easy-to-use personality.

In 1997, PHP was rewritten and the acronym was changed to “PHP: Hypertext Preprocessor”. This was PHP3.

When PHP4 was released, it was very significant, as it was the first PHP engine to be powered by Zend, The PHP Company.

Soon after, PHP5 was released, powered by an updated Zend Engine, Zend Engine II. It provided improved support for Object Oriented Programming, a consistent data access layer name “PDO”, and many performance increases.

Get To The Code Already!

Now that you know a little more about PHP’s history, let’s get started with the basics of PHP.

PHP is a server side programming language; this contrasts with a client side language, in that all PHP code is parsed and executed on the server and then the results are passed to the client. A client side language is where the raw file is given straight to the client, rather than being parsed by the server and then given. This code is then executed in the browser, on the client machine.

To run a Server Side Program, we need to install a basic web server, such as Apache, IIS, Lighttpd, or nginx.

There are four main components of a Basic PHP Application: Variables, Functions, Control Structures, and Markup. While markup is not specific to PHP, to use PHP properly, you do need to understand what makes markup. As an indepth tutorial on markup is outside the scope of this tutorial, check back later for a more indepth tutorial on Markup itself.

Variable
A piece of memory that holds a mutable value.

Function
A single unit comprised of one or more lines of code to accomplish a single task.

Control Structures
A construct within PHP that tells the program how to execute, such as what to do when certain conditions occur, or how many times to do a repeated action.

Markup
A definition of how data is to be displayed in a browser.

Variables

At first, this concept of this thing that can be changed seems rather useless. Why not just write the value you want in there? The problem you face with that is that sometimes, you won’t know what the value of the data will be for sure. A good example is my Contact Form; when the application sends me an email, it would be pretty useless to hardcode every value in there. By doing that, I’d never know who is sending the email, what they wanted or anything related to that. So having this piece of memory that can be changed is pretty useful.

Let’s look at an example:

$full_name = $_POST['full_name'];
$subject = $_POST['subject'];
$email_from = $_POST['email_from'];
$body_content = $_POST['body_content'];

Whoa, wait a minute: what is that “$_POST” thingy? Well, $_POST is what is known as a superglobal; there are 9 of them in PHP, but I usually only use, at most, 5 of them.

The 3 superglobals I use usually are “$_POST”, “$_GET”, “$_SESSION”, “$_FILES” and “$_SERVER”. Both Post and Get are arrays which contain data submitted by a user when they click a submit button on a form.

Server is an array which contains data about the server that PHP is running on. This can be useful for figuring out where a request came from, what the current script is, and other things.

Session is an array of data that is remembered across HTTP requests, and Files is used to store information about uploaded files. For more information, check the PHP Superglobals Page.

That might seem pretty stupid to assign a variable to a variable; what’s the point? That’s where the next part picks up:

Functions and Control Structures

Let’s say that you want to validate the $email_from variable, but not modify the original. When we send an email, the from address needs to be valid, so let’s validate that with PHP.

$full_name = $_POST['full_name'];
$subject = $_POST['subject'];
$email_from = $_POST['email_from'];

if( filter_var($email_from, FILTER_VALIDATE_EMAIL) != true ){
 $email_from = "mail@kkeiper.com";
}

$body_content = $_POST['body_content'];

$was_sent_successfully = mail( "to@address.tld", $subject, $body_content, "From: $email_from\r\n" );

/**
 * The "!" character inverts a condition. It is read as "not"
 * This is equivalent to 
 *
 * $was_sent_successfully != true
 */

if( ! $was_sent_successfully ){
 echo "<p>There Was An Error Sending Your Message</p>"
}

Here, we introduced 2 parts of PHP: Functions and Control Structures. When execution of the PHP script reaches the line with “if statement” on it, it runs the filter_var function.

When filter_var completes, it evaluates to 1 of 2 conditions: true or false. This is determined by whether $email_from contains a valid email address.

If the filter_var does not succeed, $email_from can’t be a valid email as is, so let’s set it to “mail@kkeiper.com”. This is good for a tutorial, but in a real situation, you would want to inform the user that their email given is not valid, so they should enter a valid email before continuing.

When we get to the bottom of the script, we see a line that assigns the results of executing “mail();” to a variable. We then check to see if the mail was sent successfully, and if not, alert the user by printing a message.

Markup

Though it is possible to use PHP without any markup, it truly is not practical. There would be no formatting to the document and it would show up as a plain text document; not very pretty.

To show information to the user, we use markup. In the example above, we wrapped a message generated by PHP in a Paragraph element and then printed it.

Again, it’s not necessary to use markup in a PHP application, but it certainly is putting you at a disadvantage not to use markup.

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

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

Thanks for your thoughts!

*