"Hello World" Explained
This lesson explains in detail the Hello World code written in the last lesson. It explains the basic syntax used in almost every PHP code.
We'll cover the following
Here’s the code from the previous lesson:
<?phpecho "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 theprint()
function.
Note: You may use parentheses with the
echo
function asecho ("string");
but it isn’t compulsory becauseecho
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.