(click anywhere to close)

[PHP] Syntax

category: Website | course: PHP | difficulty:

Like any proper programming language, PHP has a fundamental syntax that is at the basis of everything. This chapter will tell you all about it.

Integration with HTML

As you saw in last chapter, PHP files are simple text files that don’t need to be compiled. You can simply write some code within the file, and load it up in the browser to see how it works. This is great, because it allows us to very quickly make changes and update our website. But, websites are always created out of HTML, CSS and JavaScript – what role does PHP play?

Well, it pre-processes the file, which means it inserts HTML into the file before it is being sent back to the browser, and rendered. This means that you could let PHP perform something like echo "<h1>A Heading!</h1>", and suddenly you have an extra heading on the page.

Even better: it doesn’t stop there. You can let PHP decide whether a certain part of an HTML page is displayed, you can use it to add or remove CSS rules from a page based on certain conditions, you can use it to dynamically add JavaScript code – anything is possible!

The PHP Block

For the browser to know when we’re typing PHP code, and when we’re using regular HTML, we need a clear and concise way to tell the browser when a block of PHP code starts and ends. For this, we have the PHP opening and closing tag:

<?php any php code here ?>

This means that a file with the extension .php, is simply a regular .html file but with the added possibility of using PHP blocks. This also means that we can do crazy things with the page using only PHP, such as remove certain parts entirely, as the next example will illustrate.

	//Because 2 is not equal to 5, the following HTML will not display
	if(2 === 5) {
<p>This is a nice paragraph!</p>

	//Instead, PHP will insert this heading into the page
	} else {
		echo "<h1>BUT THIS IS A HEADING!</h1>";

Statements & Blocks

Anything you write, will either be a statement or a block.

  • A statement is a line of code that is to be carried out by the server. These include things such as printing a certain line, or performing some math.
  • A block is a set of statements with lines of code that open and close the block. These include things such as blocks that should be repeated several times, or blocks that should only be executed if a certain condition is true.

A statement always uses the following general syntax. Note the semicolon that closes off the statement.


A block always uses this general syntax.

someblockopening { lots of statements }
//BLOCK: If a user is logged in
if($userIsLoggedIn) {
	//STATEMENT: Display a welcome message
	echo "Welcome, " . $username;

You could leave out the last closing tag or semicolon of a PHP code block, because it will automatically be interpreted as the end of the last statement. But, it’s bad practice, and I advise against it.


Just like HTML (and CSS, and JavaScript), PHP has the possibility of adding comments within the file. These comments are not executed or parsed by the server, and are merely there for you to remind yourself why you did something or how some part of your code works. As always, the syntax for comments is the only thing in the language that is distinct from the statement or block syntax.

To create comments that span a single line, use one of the following two syntaxes

// this is a comment # this is a comment

To create comments that span multiple lines, use

/* this is a comment */
 * The following code does blabla 
 * and some more blabla
 * and finally returns blablabla

//This is a quick remark, and therefore a single-line comment

PHP discards HTML comments. So, if you created an HTML comment with the regular <-- --> syntax, and placed a PHP block inside it, it would still execute.

Do you like my tutorials?
To keep this site running, donate some motivational food!
Chocolate Milk