PHP Beginners Tutorial

Welcome to the PHP Tutorial!

Definition

PHP is a server-side programming language and is referred to as a pre-processor for HTML. PHP is a very powerful resource that many websites use, including WordPress!

Using php

Ok, so PHP is the answer for my new dynamic web application, But how do I use it?

To start working with PHP, we need to set up our web development environment. The most efficient and productive (and sometimes only way) is to set up our apache localhost server. Check out the How to Setup a Localhost  post on how to set up a Web Development localhost environment.

Delving into php

Once we have our localhost setup, we can delve into PHP programming. Open up your favourite text editor and we will start with our basic HTML5 document structure.

Implementing php

PHP code is easily implemented with these opening and closing tags:

	<!--?php?-->

We are going to start out our PHP endeavor with… You guessed it, hello world!

Hello World

So, without further adue, Hello World.

	
      
        
          
          
        
        
          <!--?php echo 'Hello World'; ?-->

Output: Hello World

Note: When using php, even with the HTML structure, you will need to save each php file with the php file extension .php. To execute the PHP file, you will need to start up your localhost servers and place the php file in the root web directory. The php file should then be able to be accessed via localhost:8080/hello.php on the Mac and localhost/hello.php on a Windows PC.

As you have probably noticed, we used the echo language construct to output some text to the browser and the statement is ended with a semi-colon.

Placing php

There are two main places that we can use PHP in our HTML document, above the HTML structure or inside the body tags.

	<!--?php /* We could put some PHP up here, but we wouldn't want to output any data up here. We could create functions up here, but we would output data in the body secition of our HTML. */ ?-->

      
      
        
          
          
        
        
          <!--?php // or we could put some PHP down here ?-->

Including php

We can also use php outside of a HTML document. We are going to take a look at including script into a HTML document with the include statement. This may seem confusing, but it’s a good idea to modularize our scripts, files and directories- essentially allowing us to keep most of our php in one file and all of our HTML in another file. Let’s create two files one named ‘script.php’ and one named ‘index.php’.

Place the following php code into the script.php file and save it in your root web directory:

	<!--?php echo "Hello from script.php"; ?-->

Now, go ahead and type out the HTML5 Document structure into your new index.php file- it should look like this:

We can now add our opening and closing tags inside the body of our HTML Document Structure and use the include statement. The include statement looks like this:

include('file/path/script.php');

Considering that our two new .php files are located in the same directory (our localhost’s root web directory), we can include ‘script.php’ into ‘index.php’ like so:

	
      
        
          
          
        
        
          <!--?php include('script.php'); ?-->

Once index.php is executed you should see the output: Hello from script.php.

Commenting in php
As you can see, I have used comments to help explain the above examples. Comments in PHP are not very different to comments in many other languages. With PHP, we have the option to use single line commenting, or multi-line commenting.

			<!--?php // this is a single line comment /* This is a multi-line comment, it looks quite different to the single line commenting doesn't it?! */ # this is a shell style of commenting, but you won't really see me in these tutorials. ?-->

Dynamic?

So, why is PHP referred to as ‘dynamic’?
PHP can store variables and take on conditional statements (i.e ‘if this equals that, then let’s do this’) among other cool things, but we will focus on variables and conditional statements for now.

Using Variables

How do variables work? There are two main types of variables available in PHP; local and global. For now, we will just be talking about local variables.

Variables allow you to assign them values. These values can be static (non changing) or they can be used to (dynamically) hold a result or value from another variable or function (I know we haven’t talked about any cool functions yet, but we will).

Each variable in PHP is defined with the $ sign (i.e $thisIsAVariable). A variable may be given a value in the same line as it is defined in. For example

	<!--?php $thisIsAVariable = 99; ?-->

Output:

Just as our last couple of PHP scripts have not output anything to the browser, this example is no different.

The last two scripts only contained comments, so that makes sense.. So, why doesn’t this variable output anything?

The above example defines our variable and initializes it with a value of 99. the variable is not being output to the browser, as we have not told the script to output anything, we only defined our first variable.

So let’s get some output this time:

	<!--?php $variableName = 99; echo $variableName; ?-->

Output: 99

Okay, so we got some output that time, but why didnt we use quotes? Quotes are not used for echoing out variables, rather just for text (strings).

Comparison Operators

PHP uses comparison operators to compare two values. An example of a comparison operator is ==.
In most programming languages, = is an assignment operator; used to assign a value to something, while == is used to compare equality.
We can also compare two values using != (not equal), > (greater than), < (less than) and a few others.

Conditional Statements
The most common statement in PHP is the infamous if statement. It may sound simple (which it is), but this is one powerful and important part of PHP!

So, let’s see a PHP conditional if statement in action

	<!--?php $variableName = 99; //defining our variable and initializing it with a value of 99 if($variableName == 99){ //if $variableName is equal to 99 then, echo 'sure does!'; // output 'sure does!' } ?-->

Output: sure does!

So the script is saying ‘if $variableName equals 99, then echo out ‘sure does!’.

So, that’s a very simple conditional statement and is only useful if we want to evaluate a variable and see if it equals 99.

What if we wanted to see if our variable is greater than or equal to 99? There are an array of logical operators in PHP, including the greater than and less than operators. These are very easily implemented into our last if. statement like so

	<!--?php $variableName = 99; if($variableName >= 99){ //this statement now says if $variableName is greater than or equal to 99 echo 'sure does!'; } ?-->

Output: sure does!

So what would happen if we changed the variables value?

	<!--?php $variableName = 101; if($variableName >= 99){ //if $variableName is greater than or equal to 99 echo 'sure does!'; } ?-->

Output: sure does!

Even though we changed the value of our variable, the if statement was still evaluated as true, giving us the output.

Here is an example of an if statement that is evaluated as false

			<!--?php $variableName = 11; if($variableName >= 99){ //if $variableName is greater than or equal to 99 echo 'sure does!'; } ?-->

Output:

As you can see, our if statement was evaluated as false. The above piece of pre, when executed evaluates the if statement and as our output (echo) is only going to be output if our statement is true, we therefore receive no output- as our if statement was evaluated as false.

The Else Statement

Isn’t there some form of fallback, to tell us the variables value or show alternate text if our statement is evaluated as false?
There sure is. Actually there are a few different methods to evaluating a value against a set of statements, but we are going to focus on the else statement.

The else statement allows us to basically show alternate text or output if our original if statement is evaluated as false.

The else statement:

	<!--?php $variableName = 11; if($variableName >= 99){ echo 'sure does!'; } else{ echo 'the if statement was evaluated as false'; } ?-->

Output: the if statement was evaluated as false

There we have it, a way to tell us if our statement was evaluated as false, or simply an efficient way of displaying alternate output.

Using Strings

We have seen that variables can hold integer (number) values, but what about text?
In most programming languages, text is referred to as a string (i.e a string of characters), PHP is no different. However, PHP does not require you to assign a data type to a variable.

	<!--?php $thisIsAStringVariable = "Hello World"; echo $thisIsAStringVariable; ?-->

Output: Hello World

Concatenation

In most languages we can use something called concatenation. Concatenation essentially allows us to ‘glue’ strings and variables (and more) together.

So let’s check this out:

	<!--?php $variable = "php"; echo "Welcome to " . $variable . "!"; ?-->

Output: Welcome to php!

Notice that the ‘dot notation’ in php is used for ‘gluing’ values together.

Functions

A function could be a simple or complex operation that we would like our app or site to do.

Here is a basic function and its implementation:

	<!--?php // function declaration function output(){ echo "this is the result of invoking our output function"; } //calling or invoking our function output(); ?-->

Output: this is the result of invoking our output function

We can also add some parameters to our new function. We will then pass these parameters into our invocation of the function. When we pass these parameters into our invocation of the function; they are called arguments.

	<!--?php // function declaration function output($arg){ echo "This is the value of the argument passed in: " . $arg . "!"; } //declaring a variable $myArg = "Ashley"; //calling or invoking our function output($myArg); ?-->

Output: This is the value of the argument passed in: Ashley!

Note: our output() function will now expect to be passed an argument.

I hope you enjoyed the shallow end of the php pool!

Interested in Developing Web Applications with PHP?

Symfony PHP Web Application Development for Beginners

At ZENVA we have a free beginners video course on PHP Web Application Development with Symfony, check it out here.