Make PHP Sites

Last updated: 2015-02-28

Working with web pages - Using PHP in HTML

In this section, we'll go over how to simplify page construction with some simple php.

Requirements

Refer to the Get Started section to know what you need to use this tutorial. You must also have an understanding of HTML and how a web page is constructed.

It helps to understand the operation of things like conditional statements, for(), foreach() and while() loops, but will not be necessary for this lesson.

Using PHP within HTML

PHP script used outside of a web page is typically structured as such:

					<?php
						$seasons = array('Spring', 'Summer', 'Fall', 'Winter');
						
						echo 'The four seasons of the year are';
						echo '
    '; foreach($seasons as $season){ echo '
  • ' . $season . '
  • '; } echo '
'; ?>

Writing PHP into html can be done that way or with an alternative method. We'll demonstrate the alternative method with that same block of code.

Alternative syntax

"Alternative syntax" is a slightly different way to write code to make it do the same things it normally would. We're going to inject the example above into a web page.

					<html>
						<head>
							<title>Page title</title>
						</head>
						
						<body>
							
							<h1>Site title</h1>
							
							<a href="">Link 1</a> <a href="">Link 2</a> <a href="">Link 3</a> <a href="">Link 4</a>
							
							<p>
							
								<?php
									$seasons = array('Spring', 'Summer', 'Fall', 'Winter');
									
									echo 'The four seasons of the year are';
									echo '
    '; foreach($seasons as $season){ echo '
  • ' . $season . '
  • '; } echo '
'; ?> </p> </body> </html>

As you can imagine, that would list the four seasons of the year in an unordered list on a page. Since this code is injected within HTML, we are going to convert it to alternative syntax.

					<html>
						<head>
							<title>Page title</title>
						</head>
						
						<body>
							
							<h1>Site title</h1>
							
							<a href="">Link 1</a> <a href="">Link 2</a> <a href="">Link 3</a> <a href="">Link 4</a>
							
							<p>
								The four seasons of the year are
								
								
    <?php $seasons = array('Spring', 'Summer', 'Fall', 'Winter'); ?> <?php foreach($seasons as $season): ?>
  • <?php echo $season; ?>
  • <?php endforeach; ?>
</p> </body> </html>

The differences

In the first version, with regular PHP, the foreach function was surrounded by {} braces. In the alternative method, the braces were replaced by a colon and the statement endforeach.

Similarly can be done with while and for loops.

Alternative syntax with conditionals

Alternative synhtaxw with the conditional if/else method is a little different.

Observe this regular syntax method on a web page:

					<html>
						<head>
							<title>Page title</title>
						</head>
						
						<body>
							
							<h1>Site title</h1>
							
							<a href="">Link 1</a> <a href="">Link 2</a> <a href="">Link 3</a> <a href="">Link 4</a>
							
							<p>
							
								<?php
									$year = date('Y');
									
									if($year > 2020){
										echo 'We are past 2020!';
									}
									elseif($year < 2010){
										echo 'We have not entered the teens of the 21st century, yet.';
									}
									else {
										echo 'We are in the teens of the 21st century!';
									}
								?>
							
							</p>
							
						</body>
					</html>
				

This is how the same operation would be in alternative syntax:

					<html>
						<head>
							<title>Page title</title>
						</head>
						
						<body>
							
							<h1>Site title</h1>
							
							<a href="">Link 1</a> <a href="">Link 2</a> <a href="">Link 3</a> <a href="">Link 4</a>
							
							<p>
							
								<?php $year = date('Y'); ?>
									
								<?php if($year > 2020): ?>
									We are past 2020!
								<?php elseif($year < 2010): ?>
									We have not entered the teens of the 21st century, yet.
								<?php else: ?>
									We are in the teens of the 21st century!
								<?php endif; ?>
								
							</p>
							
						</body>
					</html>
				

Notice that, with this conditional statement, the only thing told to end is the if statement, not each piece of it.

Why use alternative syntax?

Web pages and PHP-only files can operate in regular syntax or alternative syntax. We use alternative syntax on web pages because it allows as little of the page to look like PHP as is possible. We consider it easier to read and work with on web pages.

Try both and figure out which method is more comfortable for you. You might notice it's more difficult to recognize where an ending bracket } should or shouldn't be than it is for an endwhile; piece.

Contact us

Questions or problems? Want to contribute to this tutorial or others?

Contact us