Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. PHPBeginner - What is a PHP Block?

What is a PHP Block? - Images: Japanese

Hey there, everyone!

You've made it through setting up the PHP execution environment — great work! Now let's actually start writing some PHP.

PHP is a language that's passionate about being embedded in HTML, so the way you write it is a bit different from other programming languages. You need to wrap your PHP code inside a block called a 'PHP block', which works kind of like an HTML tag. Let's start by taking a look at how that works.

But first, let me introduce one function: echo. You may have seen it pop up in earlier articles. It's a function that outputs a string. We'll cover it in detail later, so for now just remember: "it prints stuff".

(Technically, echo isn't a function — it's a 'language construct'. You don't need to worry too much about that distinction, but just a heads-up!)

Take a look at the following source code. It's a simple HTML5 document with PHP embedded in it.

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>PHPテスト</title>
</head>
<body>
<?php
	// Write your PHP here
?>
</body>
</html>

Inside the body element, you can see '<?php' and '?>'. That's the 'PHP block'. <?php is the opening tag and ?> is the closing tag. Anything written inside the PHP block is processed as PHP code. Anything outside the PHP block is treated as plain text and output as-is. This structure exists because PHP is a language designed to be embedded in HTML, so it needs a clear way to distinguish the PHP parts from everything else.

Now let's add a 'Hello world' program to that code. It looks like this:

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>PHPテスト</title>
</head>
<body>
<?php
	echo 'Hello world';
?>
</body>
</html>

Save this as index.php, start MAMP, and go to http://localhost in your browser — you should see 'Hello world' on the page. That's the basic way to write a PHP program. You just wrap your PHP code in a PHP block, so it's nothing too tricky.

Now let's go over some important gotchas when using PHP blocks. The first thing to be aware of is that the world inside and outside the PHP block are completely different. For example, if you write 'あいうえお' inside a PHP block, you'll get an error right away. Outside the PHP block, it's just plain HTML, so that's fine.

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>PHPテスト</title>
</head>
<body>
<!-- This will cause an error. -->
<?php
	あいうえお
?>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>PHPテスト</title>
</head>
<body>
<!-- This is fine. -->
<?php

?>あいうえお
</body>
</html>

There's another easy-to-miss pitfall: if a file ends with a PHP block, you should not write the PHP block's closing tag.

What does that mean? Well, say you want to use PHP to output the closing </html> tag. Based on what we've covered about PHP blocks, you might write it like this:

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>PHPテスト</title>
</head>
<body>
</body>
<?php
	echo "</html>";
?>

This style is not ideal. The recommended way to write it is:

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>PHPテスト</title>
</head>
<body>
</body>
<?php
	echo "</html>";

Notice that the closing ?> is not there. When a file ends with a PHP block, it's best practice to omit the ?>.

Why? It comes down to how PHP works. PHP outputs everything outside a PHP block as plain text — so if there's a newline character after ?>, that newline gets output too. This can cause unexpected behavior or even errors.

That said, if you run the "not ideal" version shown earlier, it will actually work fine in many cases:

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>PHPテスト</title>
</head>
<body>
</body>
<?php
	// This actually works without problems.
	echo "</html>";
?>

For simple output like this, adding ?> followed by a newline at the end usually won't cause any issues.

However, when you include a PHP-only file (such as a file that defines functions), any newline characters after ?> get pulled into the output stream and can trigger errors. That's why files ending with a PHP block should not include the closing tag.

If you really can't stand leaving it off, make sure the file ends right at ?> with absolutely no newline after it.

<?php
echo 'Hello world';
/* If you must use a closing tag, end the file right here with no newline after it. */
?>

(That said, since it's an easy source of subtle bugs, omitting the trailing ?> is the recommended approach.)

PHP blocks also have a 'short tag' form where you use <? as the opening tag and ?> as the closing tag:

<? // This is another way to open a PHP block.
echo 'Hello world';
?>

However, this feature is disabled by default in older PHP configurations and cannot be used without changing settings. Keeping it disabled is generally considered good practice.

The reason is that it becomes indistinguishable from an XML document declaration, which looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

As you can see, <? and ?> are used right there. So PHP could get confused about whether it's a PHP block or an XML declaration — which is why the short tag is considered something to avoid.

That said, in recent versions of PHP the short tag is enabled by default.

The reasoning is that while PHP often generates XML files, it's uncommon for an actual XML file to be named .php and run through PHP processing — so disabling the short tag was seen as largely unnecessary. Personally, the author doesn't have a strong opinion either way.

Opinions are divided on this one, but the majority view is "don't use <? and ?>". So when writing PHP professionally, sticking to <?php and ?> is probably the safer choice to avoid unnecessary trouble.

And that covers PHP blocks and the basics of writing PHP. We've taken a quick look at writing some PHP, but what's actually happening behind the scenes is still a bit of a mystery. In the next article, we'll dig into 'CGI' to shed some light on that.

See you in the next one!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .