PHP for Beginners Part 1

So you want to become a PHP developer. And not just any ole' programmer, but someone who writes great code and can work on exciting projects. Well then, this screencast series from our partners at SymfonyCasts (formerly KnpUniversity) is for you! You'll learn how to develop with PHP from the very beginning, with a real project, and coding exercises throughout the lessons so you can practice immediately.

In this first part of several series, you are going to learn to:

  • Create your first PHP file
  • Use functions, and variables
  • Work with arrays, loops, and if statements
  • Read and update files
  • Handle JSON
  • Set up your computer to run PHP
  • Create your own functions
  • Organize your project into multiple files
  • Create a simple layout
Tutorials in this course
More information

So you want to become a PHP developer. And not just any ole' programmer, but someone who writes great code and can work on exciting projects. Well then, this  series from our partners at KnpUniversity is for you! You'll learn how to develop with PHP from the very beginning, with a real project, and coding exercises throughout the lessons so you can practice immediately.

In this series, we'll learn PHP from scratch by building a real website. This means you'll learn the practices used by developers to build really cool things, and not just a bunch of theory. We're going to build a site that we're calling AirPupnMeow.com. Imagine a site like Airbnb.com, except where people rent cute pets instead of apartments. If you're looking for companionship without all that responsibility of walking your dog every morning and bringing a bag to pick up his... uh gifts, then this site would be for you! Ok, the idea might be kinda silly, but that hasn't stopped startups in the past!

In this first lesson we are going to dive right in and create our first PHP file, play around with variables, and also look at what happens when we make an error, and how to read the error message we get. If you want to follow along with the steps here, you will need access to a web sever where you can write PHP files. You can get set up quickly with a number of local development servers. We have lessons that cover material for different operating systems in our Development Environments topic page.

Throughout this first series of PHP for Beginners, you are going to learn to:

  • Use functions, and variables
  • Work with arrays, loops, and if statements
  • Read and update files
  • Handle JSON
  • Set up your computer to run PHP
  • Create your own functions
  • Organize your project into multiple files
  • Create a simple layout

Additional resources

Development Environments

More information

We already know what to write when we want to use some PHP code, how to set a variable, and how to print things. Like most languages, PHP also has functions that make it easier to do more complex tasks. Instead of you needing to figure out all of the logic to accomplish something, like randomizing a number, you can just use an existing PHP function, like rand(), to do the work for you. In this lesson, you'll learn how to use a function, what arguments are and how they work, and use some examples in our demo site code.

Additional resources

PHP Documentation

More information

We've seen how we can create variables and set each to a string or a number. We've also used functions like rand and strtoupper to return numbers and strings. Let's talk about a third type of variable in PHP: an array. An array represents a group of things, like 5 random numbers or 3 strings. They're a really important type of data in PHP, and you'll be using them often. In this lesson we'll create an array, and then use foreach to loop through the array so we can complete a repetitive action for each item in the array. We'll also look at how you can access a specific item in an array, explaining keys and indexes.

Additional resources

PHP Manual: Arrays

More information

We have a basic array for our pets, but we can make these much more friendly by specifying our own array keys. We'll do just that, and we'll also see how to add new items to an already existing array, without having to go in and manually add it to the array in the code.

Additional resources

PHP Manual: Arrays

More information

Every key in an array is either a string or a whole number, which we programmers and mathematicians call an integer. And that's the end of the story: array keys are only ever strings or integers in all of PHP. But each value in an array can be any type of PHP value. So far we know three data types in PHP: a string, a number and an array. This means that we can have multi-dimensional arrays: an array with another one inside of it. Multi-dimensional arrays are actually pretty common and easy to use. In this lesson we're going to expand our existing array by putting the pet details inside the array. Once we have the new data in the array we'll look at how we access this nested information, and how we can count what we have in there.

Additional resources

PHP Manual: Arrays

More information

Right now our pet data array is just hardcoded and not dynamic. Eventually, we're going to pull this array from a database and let users add new pets to it. But before we get there, let's pretend someone has given us a file that contains all of adorable pets available on airPupnMeow.com. Our goal will be to read that file and turn its contents into a PHP array that looks just like the one we're creating now by hand. Once you add the sample JSON file to our project (found in the resources folder of the sample code), you'll learn how to open and read the file, and then decode the JSON into an array. You're also going to learn a little bit about debugging when working with files and file paths, and be introduced to PHP booleans.

More information

When you reference a key on an array that doesn't exist, PHP will complain. Instead, let's code defensively. In other words, if we know that it's possible that the age key might be missing, we should check for it and only print the age if it's there. We need to add some logic. To do this, we'll finally meet the wonderful and super-common if statement. Like foreach, it's a language construct, and is one of those things that uses curly braces to surround a block of code. Where foreach accepts an array and executes the code between its curly braces one time for each item, if accepts a Boolean value — in other words: true or false. If what you pass it is true, it executes the code between its curly braces. In this lesson we'll use the if and else statements, and see how to combine our conditions, along with a variety of operators, to get exactly what we need.

More information

When you develop a website, it usually looks something like what you've been watching me do. You open up a PHP file in some sort of editor, then point your browser at some web address that executes the PHP file and shows you the output. Now, it's time to get your computer set up so that you can get programming, if you haven't already gotten set up and been following along. In this lesson we're going to explain how a web server delivers a page to a browser and then install a web server with PHP, using XAMPP. If you would prefer to use another web server package, we have a variety of operating system packages in our Installing a Local Development Environment guide.

Additional resources

More information on Development Environments

More information

You've come a long way already, which has included using a bunch of existing, built-in functions. Now it's time to make our own! The first thing our PHP code does right now is read our pets.json file and decode it into an array, which we set on our $pets variable. Let's invent a new function called get_pets() that will do this work for us and return the finished array. Putting this into a function lets us reuse this logic whenever we want, in other places in our code, without having to write it all out again. This also makes organizing and updating our code more straight-forward and simple.

More information

We want to update our contact page with the number of pets we currently have, which means we'll need to dynamically count our pets. We can use the get_pets() function to get us that information, but we're going to have to learn how to load the function from one file for use on another file. We're going to create a new home for our functions, in a functions.php file, to keep them centralized, and then we will use the require statement to use that file when we need it. We'll also discuss the other statement you can use in this instance—require_once, include, and include_once—and why we're choosing require over the others.

Additional resources

Tip: Why is not closing your PHP tags better when you don’t have to? Great question — see StackOverflow: Why would one omit the close tag?

More information

To wrap things up on this project, we want to update the contact page with the same HTML layout as we have on the home page. To do this, we'll move the HTML sections into their own PHP files, and use require again to get them into the right places.

This course appears in the following guides:
Categories
Theming
Drupal 7, 8, 9, and 10