For this exercise, you will need:
- Access to an online PHP interpreter. I’ll be using OneCompiler for this example. View my quick overview of OneCompiler
- A cup of coffee ☕️
1. The PHP tag
You’ll notice that first line of your script looks like this
<?php
And the last line, like this
?>
- The <?php is your opening PHP tag. This tells the webserver that, instead of just spitting out whatever text it sees, to start trying to do PHP stuff with it.
- The ?> is your Closing PHP tag. This tells the server to stop doing php and start spitting out regular text (or HTML) again.
Some scripts will only contain 1 set of php tags, but you will often see many pairs of php tags sprinkled throughout a page, especially in template files! That’s because PHP was designed as a templating language, and one of the things it’s great at is doing php stuff on the fly to make template files dynamic. We’ll talk about this a lot more later on, in the sections on Templating and Includes.

Many script will have lots of php tags, especially templates! It’s also okay to have short expressions on line, closing tag and all
Important note: If your file only contains one open php tag and no HTML, it is generally considered best practice to leave it open! This is because closing it, if there are empty lines of text after it, can cause errors!

Having only one opening php tag is perfectly acceptable, and often preferred, so long as the file contains only php code. We’ll talk about this later the advanced section Separation of Concerns.
2. Frustration is the mother of all convention
You may have noticed that the echo statement is indented a little more than the opening tag. Indentation is used to establish a visual heirarchy within your scripts you’ll see it often in loops, classess, conditional statements and lots of other places. It is a convention that you will see often in many other programming languages, and in some your code won’t execute at all if not properly indented.
It is not strictly mandatory that your code be properly indented, but it makes your code much more readable. There are only 3 difficult things programming:
- Naming things
- Cache invalidation
- Remembering what the hell you did to that make that script work 6 weeks ago
Number 3 becomes nearly impossible if you can’t read the code you (or often someone else) wrote. Developers use conventions to make their code more readable. Learning and following these conventions, while a little frustrating at first, will make your experience so much better in the long run.
3. Try it out
The best way to learn code is to do it! Try things out. See if you can break things. See what happens when you do break things.
Try each of the following in the onecompiler text editor (using the default Hello World example)
Exercise 1 – Remove the opening quote from “Hello World” –
Note the the error mesage that appears in the output panel. Get used to it! Embrace it! Learn to love it even. You’ll be seeing it a lot as you learn. Learning how to troubleshoot and resolve errors is one of hte most valuable skills you can develop as a… er… developer.
Tip: Interpretting errors can be really unintuitive in php. For example, the error that you get when running this exercise
PHP Parse error: syntax error, unexpected '!', expecting ';' or ',' in HelloWorld.php on line 2
Doesn’t really indicate, in anyway, what the problem really is. It just tells you there is a syntax error. It doesn’t tell you there is a missing quote, it just tells you that there is syntax error on line 2. It’s up to you to use your experience with the language to figure out which syntax rule you violated.
In the section about Errors and error handling, we’ll talk more about some ways you can make errors easier to debug and we’ll dig more into syntax as it comes up