Coding - PHP

Introduction to PHP

PHP is one of the most used languages on the web today, and one that every web developer should have in their arsenal. This post will serve as an introduction to PHP. If you have ever programmed in another language, or have a basic understanding of HTML and the web, this should be enough to get you started building your own dynamic web pages with PHP.

What is PHP?

PHP stands for PHP Hypertext Protocol. (No, really. It stands for itself. That’s the sort of joke you can expect from computer scientists.) It’s a web scripting language, which basically means that it uses its own built-in functions, plus custom ones generated by users, to output HTML to web users. It’s a server-side language, which means that all the work goes on behind the scenes. Unlike a client-side language like Javascript, once a PHP is loaded in a browser, all of its functions have already run.

PHP is made up primarily of functions and variables. Variables are data structures that hold a specific kind of information, like a string of text or a number. In PHP, variables are labeled with a $. If you see the following code – $var1 = 10; – that means that a variable called $var1 is being declared and given the value of 10. Functions are the pieces of code that actually do the work in the script. We’ll get to examples of functions a little later in this tutorial.

Ups and Downs of PHP

There are some great advantages, and some downsides, to programming in PHP. On the positive side, it’s one of the easiest languages to learn and certainly one of the most common. The biggest content management systems (CMS) on the web (WordPress, Drupal, Joomla) are all built on PHP. And, being an open source language, there is a huge community of developers and an almost endless supply of tutorials (like this one) to answer any questions that come up during a project. That’s not always true of the smaller or proprietary languages. It’s also extremely flexible. There are often many different ways to solve the same coding problem.


The 3 biggest CMS packages all use PHP

But as widespread and easy as PHP is, it’s not perfect. Many programmers feel that the flexibility of PHP is a bug, not a feature. Debugging can be difficult, especially when coming in to work on code that someone else has written, because there are so many ways to do the same thing. The core team that updates and develops the language is sometimes seen as chaotic, and many developers will tell you that there are far more functions in PHP than there might be in a more strictly managed language, which can lead to confusion and bad code.

Still, I’ve been programming in PHP (along with Javascript and others) for almost a decade and believe that it is, with all its faults, a great way to produce dynamic web experiences.

Download and Installation

I’m not going to go into too much detail here, since most web hosts will come with PHP installed standard. If you want to install PHP on your own server, you can find detailed instructions and the latest stable version for download at

Get Started

A basic PHP document is easy to set up. One of the things that makes PHP so easy for beginning programmers is the ability to slip PHP code inside a static HTML file. To start, take this basic HTML file:

<!DOCTYPE html>
  Hello PHP!

By adding a simple set of PHP opening and closing tags, you can insert PHP into this file and begin giving it dynamic functionality:

<!DOCTYPE html>
  Hello PHP!
  <?php echo 'Hello!'; ?>

We’ll get to what that “echo” means in the next section, where I’ll get you started on PHP functions.

Your First 10 PHP Functions

There are over 1,000 functions built into PHP, and they do everything from simple text display to parsing XML and communicating with databases. I’m going to quickly introduce you to ten of the most basic ones here. If you understand these ten functions, you’ll already be on your way to building your own PHP applications. After I list them, I’m going to show you a code example that includes all ten.

1. echo

This is the simplest function in PHP, and the one that you will probably use most often in your PHP programming career. echo displays HTML code and text. That’s all. The code echo 'Hello!';
in the code example above simply writes Hello! into that point in the HTML code. What makes echo better than plain HTML is that it can display both text and PHP variables.

2. array

Echo is basic, but it’s not much fun. Let’s get into some things that HTML can’t do for itself. Arrays are data maps that can store information in simple or complex structures. The array function allows the programmer to set up an array to be used for some other function later in the code, and a simple example would be: $shoppinglist = array(eggs, milk, strawberries);. Notice how an array is really just a complex variable, with the same number-sign declaration rule.

3. date

It’s not the sexiest function in the world, but nearly every application makes use of the date in one way or another. PHP’s date function allows you to show the date in any format and use it for mathematical or other purposes. The date and time functions in PHP have their own special codes for formatting, listed here, but a simple example would be: echo date('dS');. This would output the current day of the month, with its proper suffix (“18th”). Notice how functions like date can be embedded inside echo to save the step of ever declaring a variable to store them.

4. foreach

This is one way to make use of that array function from above. Using foreach, you can execute the same chunk of code once for each item in an array. It’s a powerful way to avoid unnecessary repetition of code. Using the $shoppinglist array example that we created above, here’s an example of how to use it: foreach ($shoppinglist as $shopitem) { echo $shopitem; }. This function would simply list all the items in the $shoppinglist array.

5. if / else

This is what is called a conditional function. It allows you to execute a certain piece of code only if some condition is met. Here’s an example of an if function that might be useful: if (date('z') = 0) { echo 'Happy New Year'; }. This displays the ‘Happy New Year’ message only on New Year’s Day each year.

6. str_replace

PHP is excellent for manipulating strings (pieces of text), and one of the most useful string functions is str_replace, which allows you to replace any matching set of characters within a given string with another set of characters of your choice. This can be useful for sanitizing user-generated content that might be inappropriate for younger readers, or for abbreviating the same text in one place and showing it at its full length someplace else. Here’s an example: echo str_replace('a', 'A', 'aunt allie arrived with an apple'). That example would output the capitalized “Aunt Allie Arrived with An Apple”.

7. strlen

Another string function, this is a simple one that can be incredibly useful. Say you want to limit user input on a form to a certain number of characters (like Twitter) or make sure that a comment is long enough to be answerable. strlen can do all that and more, by telling you the length of a given string. For example, the code $length = strlen('testing'); would create a variable called $length and give it a value of 7, the length of the string ‘testing’.

8. round

PHP has a long list of mathematical functions, from square roots to hyperbolic tangents. But one that might be most likely to come up for a beginning programmer is round, which, as the name suggests, rounds a number to whatever level of precision you want. The code $estimate = round(150.001, 0) would take the number 150.001 and round it to 0 decimal places, making the value of the new variable $estimate equal an even 150.

9. is_numeric

Sites that collect user-generated content or use HTML forms often need to evaluate what kind of data a certain variable contains. is_numeric is the most simple of the functions used to make this sort of evaluation. Simply put, it will return TRUE if the variable is a number (integer, decimal, or even hexidecimal) and FALSE if it is not. In the example if (is_numeric(15f92k484k4)) { }, any code placed within the brackets would not be executed because the string being tested is not, in fact, a number.

10. isset

This function, like is_numeric, is boolean, meaning it returns a TRUE or FALSE answer. Rather than testing a variable’s content, though, isset checks whether the variable exists and has content at all. This is one that is best shown in context, so let’s include it near the beginning of our combined code example, which is coming up next.

Code Example

All right. Here’s where the rubber meets the road and you get to see your first ten PHP functions in action together. Take a look at the code and see how much of it you can understand on your own first, then have a look at the explanation below.

<!DOCTYPE html>

  <hr />
   $test = 'This is an unnecessary test';
   $items = array(1, 5.8, fire, i, test, strawberries);

   echo 'The list of items for this year - '.date("Y").' - is: 
'; if (isset($items)) { foreach ($items as $item) { if (is_numeric($item)) { echo '- A number, originally '.$item.' and rounded to '.round($item, 0).'
'; } else { if (strlen($item) > 1) { echo '- A string of text, '.$item.'
'; } else { $item = str_replace('i', 'THE LETTER I', $item); echo '- '.$item.'
'; } } } } ?> <hr /> </body> </html>

Don’t worry if that looks a bit like nonsense now. Look for each of the ten functions introduced above and see how they work. echo displays code, text, and variables. array creates a data structure to store related information, like a list. date retrieves date information, in this case, the current year. foreach lets you take apart the array and analyze each of its items. if and else allow you to test a variable to see whether it meets a certain condition. is_numeric and isset are conditions being tested by the if function. round lets you round off a numeric variable to however many decimal places you want, in this case none, making it a whole number. strlen tells you the length of a string of text, and str_replace lets you change its content.

Take a guess as to what the output of this code might look like on a web page, then take a look below for the actual output:

The list of items for this year – 2014 – is:
– A number, originally 1 and rounded to 1
– A number, originally 5.8 and rounded to 6
– A string of text, fire
– A string of text, test
– A string of text, strawberries

Keep experimenting with these ten functions, then add more to your PHP vocabulary. Each one will expand what you can do with this flexible and powerful programming language. Stay tuned here at for more tutorials, and happy programming!

About the author

Ian Rose is a web developer, blogger, and writer living in Portland, OR. In his day job, he develops WordPress plugins and custom PHP solutions, focusing on nonprofit clients. By night, he attempts to write both fiction and nonfiction. Ian's site is Seaworthy Web Solutions

Share this post

Leave a Comment

Subscribe To Our Newsletter