Echo in php – PHP Lessons

Echo command in php serves to output text. You will use the echo command more than any other. Let’s take a closer look on it!

Output line

To display the line use echo. You can use it to display the text in quotes, variables, etc. Let’s look at an example.

$myString = "Hello!";
echo $myString;
echo "<h5>I love using PHP!</h5>";

We already did “Hello World!” output but the following command echo contains html tag. Use html tags are not prohibited as php gives us the dynamics and statics page decoration is still on ordinary html.

Carefully double quotes!

You should be careful when using the HTML-code or any other string containing quotes! In Echo uses quotes to define the beginning and end of the line so you must use one of the following if you want to use quotation marks inside the echo:

  • Do not use quotation marks inside the string.
  • Put internal quotation marks after the slash. To avoid processing internal quotation marks, just put a backslash in front of them, that is \ “
  • Use single quotes (apostrophes) on the inside of the line.

See the example below for the correct and incorrect use of the ‘echo’:

// This won't work because of the quotes around specialH5!
echo "<h5 class="specialH5">I love using PHP!</h5>";
// OK because we escaped the quotes!
echo "<h5 class=\"specialH5\">I love using PHP!</h5>";
// OK because we used an apostrophe '
echo "<h5 class='specialH5'>I love using PHP!</h5>";

Output variables

Output variables is very easy. To output variables in php nothing special is needed. Here is an example that will help you to understand this:

$my_string = "Hello Bob. My name is: ";
$my_number = 4;
$my_letter = a;
echo $my_string;
echo $my_number;
echo $my_letter;


Hello Bob. My name is: 4a

Leave a Reply

Your email address will not be published. Required fields are marked *