Here's a shocker for you. PHP code is kept in files. I'm sorry, but it's true! You are going to be working with lots and lots of files. Well actually, sometimes one file, but later you'll be working with many, many files!
Now that we have that shocking truth out of the way, isn't it time that you learned how to create a PHP file.
Dayle, I understand the fundamentals of a computer file system.
Well done buddy! Good for you, but that's not where we're going with this. You see, most PHP files have something in common. I'm talking about the PHP script tag.
Take a close look at this little fella.
Beautiful isn't she? What a glossy coat. An absolutely fantastic specimen.
What? You don't feel the same about her? Trust me, after many years of development in PHP you will find her quite beautiful. You'll see her when you close your eyes to go to sleep at night. She's your best friend. She lets you use PHP.
I always prefer to lead with a practical example, so let's try something together. Create a new file, and call it
test.php. PHP files usually have the
.php extension. In honesty, we can execute them without it, but you should stick to it because if you don't then the bigger developers will laugh at you, steal your lunch and make you cry. Just kidding... developers are a friendly bunch, but you really should use the
First of all, let's write the words...
...into the file, and save it.
Great, now let's execute the file. We can use this by calling the
php application at the command line or unix shell, and passing the file name as a parameter. For example, on my Mac I'll be typing the following.
You'll see the words
Pandas rule! outputted to the screen. This is because everything outside of our beautiful PHP tags are outputted when the application is executed. Let's try something else. We are going to use our first PHP tag.
Let's edit the file so that it reads as follows.
<?php // Pandas are awesome! ?> Pandas rule!
Let's execute the file again. What's the output that we get?
Hey wait! Where's the rest?
Well spotted, my soon-to-be developer! There's a section of our file missing. This is because everything between our PHP tags is treated as PHP code, and is processed accordingly.
So what are the PHP tags? Well you've met the PHP opening tag already. Do you remember our beautiful friend
<?php tag marks the beginning of our PHP code. So when does it end? Well that's where the
?> tag comes into play.
Now that you know how the PHP tags work, it's easy for us to spot the PHP code in this file. It's the following line.
// Pandas are awesome!
So what does this line do? Absolutely nothing. It's known as a comment. It helps developers to document their own code. Don't worry. We'll learn more about comments later.
Well that was a nice short chapter, wasn't it? Now it's time for some good news. In the next chapter you'll be writing your first real lines of PHP code.
Excited? Then why wait! Flip that page.
My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.
It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new books from the comfort of my sofa!