Echo Function In Php

PHP - Echo :

As you saw in the previous lesson, the PHP function echo is a means of outputting text to the web browser. Throughout your PHP career you will be using the echo function more than any other. So let's give it a solid perusal!

Outputting a String :

To output a string, like we have done in previous lessons, use the PHP echo function. You can place either a string variable or you can use quotes, like we do below, to create a string that the echo function will output.

CODE/PROGRAM/EXAMPLE
//PHP Code:
<?php
$myString = "Hello!";
echo $myString;
echo "<h5>I love using PHP!</h5>";
?>

//  O/P : Hello!
    I love using PHP!

In the above example we output "Hello!" without a hitch. The text we are outputting is being sent to the user in the form of a web page, so it is important that we use proper HTML syntax! In our second echo statement we use echo to write a valid Header 5 HTML statement.

To do this we simply put the <h5> at the beginning of the string and closed it at the end of the string. Just because you're using PHP to make web pages does not mean you can forget about HTML syntax!

Careful When Echoing Quotes :

It is pretty cool that you can output HTML with PHP. However, you must be careful when using HTML code or any other string that includes quotes! The echo function uses quotes to define the beginning and end of the string, so you must use one of the following tactics if your string contains quotations:

  • Don't 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.

Check below example for the right and wrong use of the echo function:

CODE/PROGRAM/EXAMPLE
//PHP Code:
<?php
// 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>”;
?>

Echoing Variables :

Echoing variables is very easy. The PHP developers put in some extra work to make the common task of echoing all variables nearly foolproof! No quotations are required, even if the variable does not hold a string. Below is the correct format for echoing a variable.

CODE/PROGRAM/EXAMPLE
//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;
?>

//O/P : Hello Bob. My name is: 4a

Echoing Variables and Text Strings :

You can also combine text strings and variables. By doing such a conjunction you save yourself from having to do a large number of echo statements. Variables and text strings are joined together with a period( . ). The example below shows how to do such a combination.

CODE/PROGRAM/EXAMPLE
//PHP Code:
<?php
$my_string = "Hello Bob. My name is: ";
$newline = "<br />";
echo $my_string."Bobettta".$newline;
echo "Hi, I am Bob. Who are you? ".$my_string.$newline;
echo "Hi, I am Bob. Who are you? ".$my_string."Bobetta";
?>

O/P : Hello Bob. My name is: Bobetta
Hi, I am Bob. Who are you? Hello Bob. My name is:
Hi, I am Bob. Who are you? Hello Bob. My name is: Bobetta

This combination can be done multiple times, as the example shows. This method of joining two or more strings together is called concatenation.

#echo_function_in_php #use_of_echo_function #PHP_-_Echo #Echoing_Variables_in_php #use_of_echo_in_php_language #print_message_using_echo_in_php

(New page will open, for Comment)

Not yet commented...