In the first chapter, we explored how PHP works in theory. Now it's time to get our hands dirty and create our very first PHP web page. You're about to write your first bits of PHP code — and experience the joys of scripts that crash your computer... 😅 What? Don't run away just yet! I promise we'll stick to the basics. No scary code monsters here, just a friendly intro to PHP programming.
Don't expect anything mind-blowing at first (the page we'll make won't do anything exciting), but it's a great way to get comfortable. Most importantly, you'll learn how to separate regular old HTML from PHP code.
Creating a PHP Page
From here on, we'll be diving into the source code of your web pages. You're expected to have a basic understanding of HTML — remember, I asked you to brush up on it in Chapter 1. If your memory's a bit fuzzy, no worries — you can find the HTML tutorial here.
To edit your web page code, you've got a few options:
- Use a basic text editor like Notepad. You'll find it under Start > Programs > Accessories > Notepad. It's technically enough to write PHP... but...
- Even better, go with a code editor that highlights your syntax (super handy) and shows line numbers (also very handy). I usually recommend Visual Studio or Visual Studio Code — they're powerful and user-friendly. Another great option is Sublime Text — lightweight, fast to install, and definitely worth a try.
Whichever editor you go with, rest easy — it won't affect how you learn PHP. The steps will be exactly the same for everyone.
Let's start by creating a simple HTML page:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Simple HTML Page</title> </head> <body> <p>Welcome to <em>SiteRaw</em>, noobs!</p> </body> </html>
So far, nothing new.
But now comes the magical part: we're going to make two small changes that will "transform" this basic HTML page into a super-sophisticated PHP site. And guess what? We won't touch the code at all. Yep, you heard that right! 🤯
Here's what to do:
- When saving the file, instead of calling it
siteraw.html
(like you normally would — see Chapter 2 of the HTML & CSS course), change the extension to.php
. So now you've got siteraw.php. Don't worry — it'll still behave exactly the same for now, and you can open it in your browser just like before. - Instead of saving it on your Desktop or in some random folder, place it inside the
/www/
directory of your web server (whether you're using AMPPS, WAMP, Uniform Server, or another).
That's it!
Oh wait — one more thing. To open your page, don't type something like "C:\Users\SiteRaw\siteraw.html" into your browser's address bar. Instead, just go to "http://localhost/". No, that's not a real website — it's a special address that tells Apache, "Hey, serve me this PHP page from the server, please!" And it looks inside your /www/
folder to find it.
If everything went smoothly, you should now see:
Welcome to SiteRaw, noobs!
Impressive, right?
Okay, okay, take a deep breath! This was just a warm-up — an important one. The point was to insert the web server into the mix. From now on, all your page requests go through it. It's the web server's job to handle the PHP on the server-side. And like it or not, this page is now ready to run PHP. In fact, that's what we're about to do... very soon.
At this point, sure, it's still just plain HTML. Why? Because there's no PHP code in it. We gave it a .php
extension and put it on the server, but when the PHP interpreter read the page, it didn't find any PHP to execute. So it just sent the page back as-is.
So now the big question is: how do we talk to the PHP interpreter? How do we give it commands?
Let's say we want to make the HTML page a little more dynamic. Instead of showing the same old message — "Welcome to SiteRaw, newbies!" — we want it to say "Welcome to SiteRaw, [USERNAME]!" And that username should change depending on who's visiting. If Arnold shows up, he sees "Welcome to SiteRaw, Arnold!" If T-800 pops by, he sees "Welcome to SiteRaw, T-800!" And everyone else? They'll get the default "Welcome to SiteRaw, noobs!"
It is possible. That's exactly what happens on SiteRaw.com when you log in. And no, there aren't 700,000+ different homepage versions for each registered user ;)
What we need is a way to communicate with the PHP interpreter. We want to say: "Hey, show the page normally, but whenever you see [USERNAME], replace it with the visitor's actual name."
And that is exactly what we're going to learn how to do in this chapter.
PHP Tags
By now you know that HTML source code is made up of tags. For example, <ul>
is a tag.
Well, PHP code is meant to slide right into the middle of that HTML. As we move forward, we'll be sprinkling bits of PHP inside our web pages. These PHP snippets will be the dynamic parts of the page — meaning, they can change on their own (hence the word dynamic).
The Shape of a PHP Tag
Now, I'm not bringing this up just for the fun of it. To start writing PHP, we're going to need a new kind of tag — a slightly odd one. It starts with <?php and ends with ?>. That's where we'll put our PHP code, and I'll show you how it all works as we go.
Here's a blank PHP tag:
<?php ?>
Inside this tag, you'll write your PHP code:
<?php /* PHP code goes here */ ?>
And yes, you can totally write it over multiple lines — actually, most of the time you'll need to. PHP code tends to sprawl a little. So this is perfectly valid:
<?php /* PHP code goes here And here And here too */ ?>
There are other types of PHP tags floating around — like <? ?>, <% %>, and so on. Don't be shocked if you see them out in the wild. But <?php ?> is the standard, the good stuff, the proper way — so that's what we'll be using throughout this tutorial.
Dropping PHP into HTML
As I mentioned, you can plop PHP tags right in the middle of your HTML. Let's go back to our previous example:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Simple HTML Page</title> </head> <body> <p>Welcome to <em>SiteRaw</em>, <?php /* PHP code goes here */ ?>!</p> <?php /* Or it could go here... or even here! */ ?> </body> </html>
Of course, this page won't do anything yet because we haven't added any real PHP code. But the key thing to remember is: whenever you want to use PHP, you open a tag like this: <?php ?>.
Yep! Anywhere at all. Not just inside the <body>
of the page — you can add PHP in the <head>
, before the <!DOCTYPE>
, after the closing </html>
tag... go wild!
You can even drop PHP inside an HTML tag (okay, it's a bit ugly, but hey — it works):
<p class="paragraph" id="<?php /* PHP code */ ?>">Hello World</p>
That's what we're about to find out.
Displaying Text
Alright, enough setup. It's time to actually write some PHP code. Exciting, right?
Okay, don't expect miracles just yet — your computer isn't going to start making you coffee. But you will get a better sense of how PHP works and how it turns into HTML code behind the scenes. This is crucial to understand, so stay sharp!
The first command you'll learn is how to display text. I'll have you try it out first, then I'll explain what's happening behind the scenes.
Open up Notepad, Visual Studio Code, or your favorite text editor, and type in the following:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>SiteRaw PHP Page</title> </head> <body> <p>Welcome to <em>SiteRaw</em>, noobs!<br/> <?php echo "I'm not a noob, I know PHP..."; ?></p> </body> </html>
Copy and paste that into your editor and save the file. Time to test it out and see what happens.
But wait — do you even remember how you're supposed to save a PHP page?
Saving a PHP Page
I went over this earlier, but a quick refresher never hurts.
Save your file with the .php
extension, like siteraw.php
, inside the /www/
folder of your web server.
/www/
directory (or one of its subfolders). Otherwise, your PHP won't run.Now that the file's saved, let's test it.
Testing Your PHP Page
The steps vary a bit depending on your OS, but the general idea is the same.
On Windows, start up your web server. Don't forget to click "Start Apache" if you haven't already.
Then open your usual browser and type this into the address bar:
http://localhost/
Nope, that's not a website. You're not connecting to the internet. "Localhost" just tells Apache, "Hey buddy, I want to run a PHP file from my local server."
A page will open showing all the files in your /www/
folder. You should see siteraw.php
in the list. Click it — and there! Your computer runs the PHP code and displays the page.
You can also skip straight to:
http://localhost/siteraw.php
That should work whether you're on Windows, Mac OS X, or Linux.
Later in the course, I'll give you an option to display your results on SiteRaw itself if you want — but I really recommend testing everything locally too.
Here's what you should see:
Welcome to SiteRaw, noobs! I'm not a noob, I know PHP...
Amazing, isn't it?
I get it — you're probably thinking, "That's it?! All that for just printing some text?!" And you're not wrong. All we've done is make PHP write a line on the screen.
Absolutely 😄 But hang tight — you'll soon see the real power of PHP. For now, we're just proving that it does exist (and no, PHP isn't a scam lol).
Let's break down that echo instruction. We've reached that moment.
The echo Instruction
Let's zoom in on the PHP part of your code:
<?php echo "I'm not a noob, I know PHP..."; ?>
Here's what's going on:
- The PHP code is inside
<?php ?>
- Inside that, we wrote:
echo "I'm not a noob, I know PHP...";
Now, PHP is a programming language. HTML isn't — it's more of a "description language" used to structure web pages (the proper term is markup language). If you've used languages like C or C#, this probably feels familiar. But since this course assumes you're starting from scratch, I'm going to keep things simple. 😉
Every programming language has what we call instructions. These are commands you give the computer, and they usually end with a semicolon ;
.
In this case:
- echo is the instruction — it tells the computer: "Display this text."
- The text itself is wrapped in quotation marks
"..."
. You can also use single quotes'...'
. - The line ends with a semicolon
;
— super important!
;
is a big no-no. So make it a habit!And yes, you can absolutely include HTML tags in your echo statements! For example:
<?php echo "Welcome to <strong>SiteRaw.com</strong>"; ?>
That'll display "SiteRaw.com" in bold.
The classic beginner trap. If you put quotes inside quotes, PHP gets confused and assumes your string ends too early, leading to a lovely parse error.
One way around it is to use single quotes ' on the outside, but that will lead to the same problem if your text contains apostrophes.
The better solution? Use a backslash \ to escape the quotation mark:
<?php echo "Welcome to \"SiteRaw.com\" everyone."; ?>
Boom — problem solved.
That's pretty much all you need to know about echo for now. Go ahead, try a few lines on your own!
Try stacking two or three echo statements in a row — just remember to add a <br />
tag to make each one appear on a new line:
<?php echo "Welcome to \"SiteRaw.com\" everyone.<br />How are you today?<br />"; ?>
Remember: HTML tags work just fine inside echo.
Now go have some fun! (Okay okay, maybe it's not the most fun you've ever had — but still! 😄)
Comments in PHP
Whew! Without even realizing it, you've just learned a whole bunch of stuff in one go. Feeling a little overwhelmed? 😄
Okay, maybe it wasn't groundbreaking just yet — but trust me, the magic is coming. You're about to discover just how clever all this can get.
Before we wrap up this chapter, there's one last thing I have to show you — something I consider super important in PHP (and in programming in general): comments.
A comment is a note you write to yourself inside your PHP code. When the page runs, that comment completely disappears. The browser never sees it, and your visitors never will either. It's just for you.
They help you keep your code readable and organized. Because believe me — after a few weeks away from a project, your own code might look like it was written by a complete stranger. 😅 With comments, you can remind yourself what's going on and why you wrote things a certain way.
There are two kinds of comments:
- Single-line comments
- Multi-line comments
Which one you use depends on how much you want to say. Let's check out both types.
Single-line comments
To write a comment on just one line, you start with two slashes: //
. Then, type your comment.
An example?
<?php echo "I'm learning PHP."; // This line says what I'm doing// The next line tells you where echo "On SiteRaw."; ?>
Here, I've added two comments in different spots:
- One at the end of a line
- One on its own line entirely
It's totally up to you where you place your comments. But if you're explaining a specific line, it's usually clearer to put the comment right after it.
Multi-line comments
Now these are your best friends when you've got a lot to say. You can use them for multiple lines — or just for one, if you want. To open a multi-line comment, start with /*
and end with */
.
Like this:
<?php /* The next line says how old I am If you don't believe me... ... you're probably right :) */ echo "I'm 97 years old."; ?>
Okay, granted — this particular comment doesn't add much value. But in the upcoming chapters, I'll be using them a lot to explain what my PHP code is doing. And you'll get the hang of it quickly too! 🙂
At this point, you might be wondering: "What's even the point of PHP?"
And honestly, fair question. Right now, it looks like we're just complicating things that could be done faster with plain HTML. But don't worry — that's about to change.
In the next chapter, we're diving into one of the most powerful tools in PHP: variables. These little creatures are insanely useful — you'll see exactly why very soon! 😄
See you there!
Enjoyed this PHP & MySQL course?
If you liked this lesson, you can find the book "How to Build a Website in HTML and CSS" from the same authors, available on SiteRaw, in bookstores and in online libraries in either digital or paperback format. You will find a complete PHP & MySQL workshop with many exclusive bonus chapters.