PhpMyanmar.com logo Php Tutorial
Home
Php Tutorial
 Echo
 If

Echo
Show Myanmar Version
 show-myanmar-font
Printer Friendly Page

The PHP function echo is a way for outputting text to the web browser.

The echo function will be used more than any other functions.

Outputting a String

The use of echo function to output to a web browser is shown below:

<?php

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

The text outputed is sent to the browser as a web page

In the second echo statement echo was used to write a valid Header 5 HTML statement

To do this simply put the <H5> at the beginning of the string and closed it with </H5> the end.

Be Careful When Echoing Quotes!

You must be careful when using HTML code or any other string that includes quotes!

  • Do not use quotes inside your string
  • Escape your quotes that are within the string with a slash. To escape a quote just place a slash directly before the quotation mark, i.e. \"
  • Use single quotes (apostrophes) for quotes inside your string.

See the example below for the correct usage of echo function:

PHP Code:
<?php

    echo "<h5 class="specialH5">I love using PHP!</h5>";
   //won't work because of the quotes around <H5></H5>

    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 '.

?>

Echoing Variables

The example shown below how variables are 'echo' out to the browser:

PHP Code:
<?php

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

    echo $my_string;
    echo $my_number;
    echo $my_letter;

?>
Display:
Hello Bob. My name is: 4a

Joining 'Variables' and 'Text Strings' and then echoing

Variables and text strings can be  joined together with a period( . ). The example below shows how to do it.

PHP Code:
<?php

    $my_string = "Hello Bob. My name is: ";
    $newline = "<br>";

    echo $my_string."Bobettta".$newline;
    echo "Hi, I'm Bob. Who are you? ". $my_string.$newline;
    echo "Hi, I'm Bob. Who are you? ". $my_string."Bobetta";

?>
Display:
Hello Bob. My name is: Bobetta
Hi, I'm Bob. Who are you? Hello Bob. My name is:
Hi, I'm Bob. Who are you? Hello Bob. My name is: Bobetta

This method of joining two or more strings together is called concatenation

Note
For more detail please see Php Manual at http://www.php.net

Processing time -- 0.0134 seconds.

726643e044eefbf9d070e6bc627342de