On September 14th, 2017, we published revised versions of our Privacy Policy, Terms of Service and Website Use Policy and published a Cookie Policy. Your continued use of Lynda.com means you agree to these revised documents, so please take a few minutes to read and understand them.

Learn it fast with expert-taught software and skills training at lynda.com. Start your free trial

By David Powers |

How Do I Learn PHP?

How do I learn PHP?

One piece of advice sticks in my mind from the days when I started learning PHP: “Just read the PHP online documentation. You don’t need anything else.” PHP’s online manual is excellent, and contains lots of practical examples. But it was like throwing me a Chinese dictionary and telling me it contained all I needed to learn the language. I had no idea where to start.

Why PHP?

According to W3Techs, PHP runs four out of every five sites that use a server-side language. Server-side means that the dynamic code is processed on the web server, unlike JavaScript, which is normally processed in the browser. This free video explains the difference.

Among the many uses for PHP are server-side includes to store common page elements—such as headers, footers, and navigation menus—in external files. You update only the external file instead of making changes in every page. PHP’s other strengths include sending form input by email, uploading files, password protecting pages, and communicating with a database. You can even manipulate images with PHP.

What PHP can’t do is change the contents of a web page without a round-trip to the server. However, you can send an Ajax request to PHP in the background.

PHP basics

PHP has a gentle learning curve, allowing you to achieve practical results once you understand a few basic concepts.

  • Variables: These are placeholders for values that aren’t known in advance, or that might change. In PHP, variables always begin with a dollar sign.
  • Arrays: Related values can be stored in arrays like a list of items.
  • Functions: PHP has thousands of built-in functions that perform preset tasks, such as getting an image’s dimensions or sorting values. You can also build your own functions.
  • Loops: Repetitive tasks, such as displaying database results, are handled by loops that run until a condition is reached (for example, the end of the result set).
  • Conditions: Scripts can make decisions by comparing values (is the total greater than, less than, or equal to a specific value?) or testing whether something is true or false.

The following code, which generates an up-to-date copyright notice, demonstrates some of those features:

$startYear = 2014;
$currentYear = date('Y');
if ($currentYear > $startYear) {
  $currentYear = date('y');
  $dates = "$startYear–$currentYear";
} else {
  $dates = $startYear;
echo © $dates;

The code is wrapped in PHP opening (<>) and closing (?>) tags, which allow it to be embedded in HTML. The first two lines inside the tags define variables called $startYear and $currentYear. The value of $currentYear is generated by the built-in date() function. The code then tests whether $currentYear is greater than $startYear. If it is, $startYear and $currentYear are used as a range of dates. Otherwise, only $startYear is used. The final line displays a copyright symbol with the appropriate dates.

Reading code like this can seem intimidating, but you’ll quickly get used to it once you’ve grasped the basic concepts.

Choosing an editor

In theory, you can use any text editor to write PHP, but your life will be a lot simpler if you choose an editor with the following features:

  • PHP code hints
  • PHP syntax coloring
  • PHP syntax checking
  • Line numbering
  • A way to match opening and closing parentheses, brackets, and braces

I’ve tested many editing programs, and have settled on Dreamweaver and PhpStorm for working with PHP. I use Dreamweaver when I’m mixing PHP into an existing website because of its support for HTML and CSS. In addition to the features listed above, Dreamweaver CS5 and later has the PHP documentation embedded in the code hints. However, don’t be tempted to use Dreamweaver’s PHP server behaviors to connect to a database. They’re out of date, and were removed from Dreamweaver CC. PhpStorm is my preferred editor for complex PHP scripts.

For advice on both programs, as well as alternatives, check out Joseph Lowery’s course, Choosing a PHP Editor, on lynda.com.

Testing your scripts

I recommend setting up a local testing environment for PHP. It’s faster and more convenient than using the remote server on your website.

XAMPP or WampServer are good choices for Windows. I use MAMP on my Mac. They’re free, and they install PHP, the Apache web server, the MySQL database, and other useful features in a single operation. I give basic installation instructions for XAMPP and MAMP in my Introducing PHP course on lynda.com. David Gassner’s Installing Apache, MySQL, and PHP goes into the setup process in greater detail.

Start developing

Even if you’ve got a particular project in mind, don’t try to dive into the deep end. Start with some simple exercises to familiarize yourself with variables, functions, and control structures, such as loops and conditional statements. Learn about PHP’s superglobal arrays, such as $_GET, which retrieves values from a URL’s query string in this free video.

Once you know the basics, it becomes much easier to adapt a script you might have found that does what you need.

I’ve created two beginner courses on lynda.com: PHP for Web Designers offers a mixture of standalone exercises and practical mini-projects showing how to adapt an existing static website by incorporating PHP features, such as server-side includes, handling data from an online form, and connecting to a database. My Introducing PHP takes a slightly more formal approach, explaining the basics of PHP before showing in detail how to send the contents of an online form by email.

As a beginner, be prepared to make mistakes. Programming languages like PHP are less forgiving of mistakes than HTML. A comma, semicolon, quote, or parenthesis out of place can halt your script in its tracks. Both my beginner courses help you diagnose what might have gone wrong.

After that, you might enjoy some of my intermediate courses, or you can take a deep dive with Kevin Skoglund’s PHP with MySQL Essential Training. You’ll also have the knowledge and confidence to explore the PHP online documentation, and understand why many say that PHP really stands for Pretty Happy Programmers.

Tags: , , , , ,

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.