0% found this document useful (0 votes)
12 views1 page

Echoing Variables: PHP Code

This document discusses using echo statements in PHP to output strings, variables, and a combination of the two. It explains that quotes need to be escaped when outputting strings that contain quotes. It also shows that no quotes are needed when echoing variables, and variables and strings can be combined in a single echo statement separated by periods.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Echoing Variables: PHP Code

This document discusses using echo statements in PHP to output strings, variables, and a combination of the two. It explains that quotes need to be escaped when outputting strings that contain quotes. It also shows that no quotes are needed when echoing variables, and variables and strings can be combined in a single echo statement separated by periods.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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>";
?>

If you want to output a string that includes quotations, either use an apostrophe ( ' ) or escape the
quotations by placing a slash in front of it ( \" ). The slash will tell PHP that you want the quotation to be used
within the string and NOT to be used to end echo's string.

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.

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

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.

You might also like