Print Text Using Echo Command

4 min read

You can print a text or statement in PHP using the language construct echo.

Create a new file for the first example (e.g., hello.php) and save the following text in it:

<?php
echo "Hello World";
?>

If you want to run the file locally, you need to save it in the htdocs folder of your XAMPP installation. More information in previous article Getting Started.

If you open the file via browser, the output will be: Hello World. The visitor’s browser receives only the result of your script, not the PHP code. It’s because PHP code is executed on the server side, and just the result is sent to the user.

As in the article Getting started, we always start a PHP script with the line <?php.

The PHP statement follows the next line.

echo  "Hello World" ;

echo is the command to output a specific text or string. This text is between the two quotation marks. The semicolon (;) ends the echo statement. Almost every command (with a few exceptions) ends with a semicolon (;). By using ?>, We conclude the PHP script.

Everything between the quotation marks consider as text and You can define as you want; also HTML commands are possible, eg.

<?php
echo "<b> Hello World </ b>";
?>

This would make the Hello World as bold text.

If you want to print Hello “World” now, many beginners first think of the following code:

<?php
echo "Hello" World "";
?>

However, the code above will produce the following error message:

Parse error: parse error, unexpected T_STRING, expecting ‘,’ or ‘;’ in C:\xampp\htdocs\hello.php on line 2

This error message is useful because it can be seen where the error was committed and usually which one. On line 2 indicates that the error is.

echo  "Hello" World "";  

So something has to be wrong here! It is quite logical. The echo command always starts the output from the first quotation mark and outputs everything up to the second quotation mark. However, since a total of 4 quotes are used for text designation in this command line, no clear definition is given. Therefore, this error message occurs and not the desired output.

So we have to put the quotation marks at “World” with a masking mark. For this, we use the backslash (\).

The script would be look like this:


<?php
echo "Hello \" World \"";
?>

This masking character must precede any quotation marks that should be output as text.

But what if we also want to include a backslash now? That is also possible, but again a masking sign is required. For this, we are able to put backslash in front of backslash (double backslash).

This looks like this:


<?php
echo "D: \\ xampp";
?>


The output would be: D: \ xampp

PHP file can also consist of PHP scripts and HTML commands, such as:


<h2> Welcome <?php echo "to my website"; ?></h2>

That’s all you have to know about the echo command.

How is the echo command used?

The echo command does not look very helpful yet. You could print a text with HTML. The disadvantage of HTML is that no dynamic texts can be output. that is different from PHP.

Even if you master PHP perfectly, all texts are output with echo (or similar). The special feature is that these texts are able to be generated dynamically, e.g., from a database. However, you get the output control, with the help of certain variables. The visitor then usually determines for himself which text he receives.

« »