Lesson 4 – How to Echo text in php
You can output a text in PHP using the language construct echo .
Create a new file (eg hello.php ) for the first example 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 our article First Steps .
If this file is called up with the browser, the following output will appear: Hello World. The visitor’s browser receives only the result of your script, not the PHP code. The PHP code is executed on the server and only the result is sent to the user.
As in the article Getting started, we always start a PHP script with the line <? Php .
The next line is followed by the PHP statement
echo “Hello World”;
echo is the instruction to output a specific text. 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 (;). With ?>We end the PHP script.
Everything between the quotation marks is considered text and can be freely defined, even HTML commands are possible eg
<? Php echo "<b> Hello World </ b>" ; ?>
So that would Hello World as bold output.
If you want to print Hello “World” now , you first think of the following spelling:
<? Php echo "Hello "World "" ; ?>
However, we then get the following error:
Parse error: parse error, unexpected T_STRING, expecting ‘,’ or ‘;’ in D: \ xampp \ htdocs \ hello.php on line 2
This error message is very useful, because it can be seen where the error was committed and usually which one.
on line 2 indicates that the error is in line 2.
In this line is this statement:
echo “Hello “World“”;
So something has to be wrong here! Actually, it is quite logical. The echo command always starts the output from the first quotation mark and prints everything up to the second quotation mark. However, since a total of 4 quotes for text designation are used 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 looks like this:
<? Php echo "Hello \" World \ " " ; ?>
This masking character must precede any quotation marks that should be output as text.
But what if we want to spend a backslash now? This is also possible, but again a masking sign is needed. For this we just take a second backslash.
This looks like this:
<? Php echo "D: \\ xampp" ; ?>
The output is then: D: \ xampp
PHP pages can consist of PHP scripts and HTML commands, such as:
< h2 > Welcome </ h2 > <? Php echo "On my website" ; ?>
This was all you have to know about the echo command .
How is the echo command used?
The echo command still does not look very helpful. You could spend a text with HTML. The disadvantage of HTML is that no dynamic texts can be output. Unlike PHP.
Even if you master PHP perfectly, all texts are output with echo (or similar). The special feature of this is that these texts are then usually generated dynamically, eg from a database. However, you then control the output, with the help of certain variables . The visitor then usually determines for himself which text he receives.
We are sorry that this lesson was not useful for you!
Let us improve this lesson!
Tell us how we can improve this lesson?