Why learn PHP?

See more about:

Where are we?

Should you spend your time learning PHP at CoreDogs? This lesson looks at part of that question: why learn PHP at all?

This lesson’s goals

By the end of this lesson, you should:

  • Know what PHP does.
  • Know how learning PHP can help you.
  • Know why it’s better to start with PHP than other languages, like Java or ASP.NET.

What does PHP do?

It’s easiest to understand with an example.

Humans have a site called Facebook. Maybe you’re a human yourself (I won’t hold it against you), and have a Facebook account.

Suppose we want to create Snoutbook, like Facebook but for dogs. One feature will be like the Facebook wall. The wall is a place where people can put text, photos, and other stuff. Here’s part of my wall (please don’t tell the Facebook people that I’m really a dog):

My wall

Figure 1. My wall

Snoutbook will have the same thing, but we’ll call it the tree.

When Ivan posts a new entry to his tree, other dogs get to see it:

Ivan's tree

Figure 2. Ivan’s tree

Here’s how it will work:

Snoutbook

Figure 3. Snoutbook

Mazie types http://snoutbook.com into her browser. The Web server is the software that sends back the HTML for that page. The best known Web server is Apache. You can use it for free. We’ll talk more about that in the next chapter.

Web servers aren’t good at storing lots of data. But database servers are good at that. So the Web server sends Ivan’s post to the database server. When Mazie wants to look at Ivan’s tree, the Web server gets the data from the database server, and formats it.

There are many different database servers. One of the best known is MySQL. It’s free, too.

But wait, wait! There’s a problem. Web servers don’t know how to talk to database servers! Arghhhhh! What to do?

Enter PHP.

PHP code can be embedded directly into HTML files, just like JavaScript.

tree.php

Figure 4. tree.php

Unlike JavaScript, the PHP statements are run on the Web server. They can do servery things, like access databases.

The files usually have the extension .php instead of .html. So if Mazie wanted to look at Ivan’s tree, her browser would ask for tree.php rather than tree.html. But the browser would still get HTML code.

Here’s what happens when Mazie looks at Ivan’s tree.

Snoutbook's architecture

Figure 5. Snoutbook’s architecture

The browser asks the server for tree.php (1). The Web server – Apache, say – loads the file tree.php from disk into memory (2). The Web server sees that the file’s extension is .php, and sends the file to the PHP interpreter (3).

The PHP interpreter is a program, running on the same computer as Apache. It knows how to follow instructions written in the PHP language. These PHP instructions are written by a person – human, dog, or some other intelligent being.

So the PHP interpreter runs the PHP code in tree.php (4). This code asks the database server for Ivan’s tree postings. The database server returns the data. The PHP code wraps the data in HTML tags, like <p> and <div>.

When it has finished running all the code, the PHP interpreter sends the result back to the Web server (5). The result will be a bunch of HTML, created by the PHP code in tree.php. The server then sends the data to the browser (6).

The browser just gets HTML. It doesn’t know, or care, that it was generated by a program.

So that’s what PHP does. PHP code runs on a server, and generates HTML (usually – it can output other types of data as well).

So why is this important?

This is a Big Fat Hairy Deal

When Mazie tells her browser to get tree.php, she sees something different each time. What she sees depends on what is in the database. If Ivan has added something, the database will have changed, and tree.php will show different content.

Server-side programming technology makes the Web what it is today. It’s the ability to store data from one person and show the data to someone else that’s important.

This technology is behind YouTube, Amazon, Twitter, Facebook, and, well, just about every big site you’ve heard of. It makes the Web what it is today.

Server-side programming is also behind search engines like Google, Yahoo, and Bing. They grab Web pages and store them. When you do a search, a program runs on one of their servers, and accesses the stored data.

These sites don’t use PHP, necessarily. People use many languages for Web programming, including PERL, ASP.NET, C, C++, C#, Java, Ruby, and Python. More on this later.

Why will learning PHP help you?

Let’s talk about how learning PHP can help you.

Understand how the Web really works

You won’t know how the Web works unless you know something about server-side processing. There’ll be a big gap in your knowledge. You won’t know how most of the Web pages you see every day are created.

Understand business value on the Web

Much of the business value of the Web depends on the server side. Want to sell products online? Your product data will be in a database. Web pages describing products will be generated by programs written in PHP (or some other language).

If you set up a Web shop, you probably won’t write your own PHP shopping cart. You’d use one somebody else has written. But if you understand how it all works, you can manage the software more effectively. Which brings us to…

Install and manage software

You have thousands of PHP applications to choose from. Many of the most popular Web applications in the world are written in PHP: WordPress, Drupal, Joomla, and many others.

If you learn a little PHP, you’ll find it easier to install and manage these applications. The documentation and forum discussions will make more sense to you.

You’ll also be able to…

Customize and extend software

You can use PHP to extend existing applications. You can change them to match your exact requirements.

CoreDogs is like this. It’s a Drupal site. Drupal is a very powerful content management system. But it doesn’t do everything I wanted.

For example, CoreDogs embeds exercises directly in pages. It shows the exercises you have completed in your portfolio. Suppose you want to share some of your solutions with, say, your cousin Jim. You can choose the exercises you want him to see, and send him a URL that lists them.

Drupal doesn’t have these features. But it does let you add your own features. This was one of the reasons I chose Drupal. To add your own “modules” (as Drupal extensions are called), you need to be able to write PHP.

Write your own programs

You can write simple PHP programs for many tasks. For example, you can build a contact page. Or a page that helps users recommend a site to a friend. Even a simple chat feature is easy to write.

CoreDogs won’t turn you into a professional PHP programmer. But you’ll find that there are many basic day-to-day things you’ll be able to do with PHP.

Work with others more effectively

If you work for a big company, you can bet that PHP or another server-side language is used on many of the firm’s sites. If you haven’t run into one yet, you will.

You might be asked to work on one of those sites, as a document creator, graphic designer, project manager, or in some other role. You might work with PHP programmers. You’ll be able to work with them more effectively if you understand the tools they’re using.

If you run a small business Web site, you might hire and supervise PHP contractors. You’ll be able to do that better if you know a little PHP yourself. You’ll also be able to explain to them what you want, and understand their answers to your questions.

Decide if server-side programming is for you

There are many different Web jobs. Designer, artist, writer, community manager, usability tester, marketer, server admininstrator,... Oh, and programmer.

Learning about PHP will help you decide how much server-side programming you want in your career.

There are good reasons why it’s worth learning server-side programming. But…

Why PHP instead of Java, ASP.NET, Ruby, ...

There are two reasons it’s best to start with PHP.

PHP is widely used

PHP is likely the most widely used server-side programming language (see this article, for example). It’s used on millions of servers world wide.

Exercise: HotScripts

HotScripts is a collection of Web programs and related stuff. They list scripts (another word for programs) by language. What language has the most scripts? What percentage of their scripts are in PHP?

(Log in to enter your solution to this exercise.)

PHP is very popular in the open source world, where applications like WordPress and Drupal live. One reason is that PHP is itself open source.

This means that your PHP skills will have value to many different people (including you!).

Exercise: PHP apps on your hosting account

Open up the control panel on your shared hosting account. If you don’t have an account, get one.

You probably have something like Fantastico, that lets you install software on your account. Here’s what part of my Fantastico page looks like:

Fantastico

Figure 1. Fantastico

These are some of the applications Fantastico will install for me.

Pick ten or so applications at random. Find out what server-side programming language they use. List the number that use each different language.

(Log in to enter your solution to this exercise.)

PHP is (relatively) easy to learn

Make no mistake: programming is hard. For most people, anyway.

What people have trouble with is not the programming languages, but the way of thinking about program design. This only comes with practice, practice, and more practice.

But still, some languages are easier to learn than others. Java is one of the more difficult. It’s a good language, but it’s very structured, and hard for beginners to get started with.

Two of the easiest languages to learn are JavaScript and PHP. Even a few lines of JavaScript and PHP can do useful things. You can start writing programs quickly, without having to learn of lot of stuff first.

Summary

PHP programs run on Web servers. They let Web pages store data on database servers. Server-side programs are crucial on today’s Web.

Learning PHP can help you:

  • Understand how the Web really works
  • Understand business value on the Web
  • Install and manage software
  • Customize and extend software
  • Write your own programs
  • Work with others more effectively
  • Decide on a career

PHP is widely used. It’s easy to learn, for a programming language.

What now?

Let’s talk about the CoreDogs Way of learning PHP. You should decide whether it’s right for you.


How to...

Lessons

User login

Log in problems? Try here


Dogs