Echo statement and comments

This tutorial introduces the echo statement for displaying messages and also discusses comments that can be added to your code. Echo statements can be used to output simple messages, variable values, and also HTML code. You can even include JavaScript code within echo statements. The echo statement is similar to the print statement you might see in many other languages.

For example, the statement echo “Hello world”; will display the message “Hello world”. The statement echo $myVariable; will output the value of the variable called $myVariable. The statement echo “<h1>My heading</h1>”; will output text formatted in a h1 size heading (using the h1 heading HTML tag).

You could even add JavaScript code to an echo statement, for example: echo ‘<script>alert(“Hello world”);</script>’;

Watch the video below and then scroll down to see the sample code:

PHP sample code:

<?php
 // this is a single-line comment
 
 /* 
 this
 is
 a
 multi-line
 comment
 */
 
 ############################
 # this is a comment block #
 ############################

 echo "Hello world"; // this is a string in an echo statement
 echo "He said \"Hello world\""; // this is an example of escaping quotation marks
 echo 'Hello world'; // this is an example of a string inside single quotation marks
?>

A reminder about the ?> tag

If you are a beginner PHP programmer, it is recommended that you omit the closing ?> PHP tag at the end of your code if the file is pure PHP code. This will prevent any unwanted effects that may occur if you add whitespace after the closing tag.

If you do use the closing ?> then it is important that you do not add any other white space (spaces or new lines) after the tag. You can read the reasons why at the official PHP documentation site.

PHP Manual references: