codeswan blog

PHP: We’re going elephant hunting!

A rare sighting of the elusive elePHPant...
Photo courtesy of Phu Son Nguyen

There is an elePHPant in the room, and we really need to address it.

No, seriously.

Now, I don’t care who you are or what you say, but if you want a website that does more than just sit there and forever stay the same, we’re going to have to talk about this elePHPant. It goes by the name of PHP, and without it your website wouldn’t do jack. You want the same header on every page? You need PHP. You want comments on your posts? You definitely need PHP. Heck, you want posts that you don’t have to code by hand? Yep, you need PHP.

(Okay, okay, so technically you could do all these things using other programming languages. However you’d still need something beyond HTML and CSS, and PHP is by far the most widespread – and the one that you’ll see in every single WordPress website out there.)

PHP is an integral part of every WordPress theme. Without PHP, your website just wouldn’t work. You couldn’t have your newest posts show up on your front page every time you updated your blog; you couldn’t have the same header, sidebar, and footer automatically show up on every page. Nobody would be able to leave a comment, and an archive that lists all your posts? Forget about it.

Well, if I’m going to be up-front with you, you could have all those things (except for the comments). But you would have to re-code huge amounts of your website every time you added a new post, and if you have a business or a day job or just plain don’t want to spend all your time weeding through code when there are much better options… yeah, you don’t want to do that.

So. Now that we’ve decided that PHP’s an excellent idea for your website (any objections? No? Good.), it would be immensely helpful to understand what all that PHP code means, right? This is where my next few posts come in… I’m going to teach you everything you need to know. For now we’re going to ignore the functions.php file, which is where all the fancy schmancy PHP code goes, and just look at the basic PHP that you find in the rest of the files in your theme.

Come Out With Your Hands Up, PHP, We Have You Surrounded

If you look in most of the .php files in your theme, like index.php or sidebar.php, you’ll see that the PHP code is mixed in with the HTML code. So how does your browser know what to render as PHP, and what to render as HTML? Just as almost every HTML element has an opening and closing tag, so does PHP. The opening tag looks like this: The closing tag looks like this:?> Everything in between the opening and closing PHP tags is... you guessed it... PHP code.

There's really nothing more to it. Everything between those two tags is PHP code, everything outside is... not. Outside you usually have HTML, or even JavaScript sometimes, but anytime you ever want to include PHP in your code, it has to be surrounded by the <?php and ?> tags.

Ta Da!

So there you have it, your very, very basic introduction to PHP.

Wait... that's it?

Oh goodness no. In the next few weeks, I'm going to have a whole heck of a lot more to teach you about PHP. Believe me, we've hardly scratched the surface. But for now, I'm just going to leave you with today's short lesson, and let you digest that for a week.

Now would somebody please do something about the darned elephant crowding us all out of the room?

Leave a Reply

Your email address will not be published. Required fields are marked *