Search⌘ K

"Hello World" Explained

Explore the fundamentals of writing your first PHP script by understanding the PHP opening and closing tags, the echo statement for output, and proper use of semicolons. This lesson lays the groundwork for coding PHP by explaining how PHP processes and displays data on the screen.

Here’s the code from the previous lesson:

PHP
<?php
echo "Hello, World!\n";
?>

Let’s take a look at this in detail.

We’ll start from the very first line and go step by step!

PHP Opening Tag #

A PHP script can be written anywhere in the document, and it signifies the start of the PHP code.

echo #

PHP uses a built-in function called echo in order to display one or more strings.

Tip: The echo function is faster than the print() function.

Note: You may use parentheses with the echo function as echo ("string"); but it isn’t compulsory because echo isn’t really a function.

PHP Closing Tag #

The PHP closing tag marks the end of the PHP code. It is written as:

Note: All code in PHP is written between the starting (<?php) and ending (?>) symbol. However, the closing tag of a PHP code block at the end of a file is optional. The code would still execute properly even if the closing tag is missing.

Semicolons #

Statements in PHP must be terminated with a semicolon.

  • Just as sentences in English must be terminated with a period.
  • Just as sentences in English can span several lines, so can the statements in PHP.

You can use as many spaces and newlines between the start and end symbol of a PHP program as you wish. It helps beautify your code just as spaces are used to align the text printed on the pages of a book.

Now that we have learned the basics let’s take a quick quiz in the next lesson.