Articles

Displaying Content in PHP

Learn PHP display content Lesson 3 Tutorials PHP

Display content outputting content to a web page

3. Display content outputting content to a web page.

You can write something in a browser using PHP using the PHP syntax echo. Additionally print() will also output content to be displayed on your web page. The format to use either one of these syntaxes to place quotes either single or double around the text you want to output. If you are using a variable you don't need the quotes.

PHP Code Sample:

<?php

echo 'Some Words';

?>

PHP output display

Some Words

As in the previous example on variables you can use the code to output the variable contents on a web page.

PHP Code Sample:

<?php

$myVarilable = 'Some Words in this variable';
echo $myVarilable;

?>

PHP output display

Some Words in this variable

When using quotes in the actual output of the syntax you must include the backslash so that PHP doesn't break out of the value.

PHP Code Sample: (both below are correct to include a single quote)

$myVarilable = 'I\'m ready to output text';
$myVarilable = "I'm ready to output text";

PHP output display

I'm ready to output text

If you want to include a variable and text you can join them using a period or in text strings you can use the variable inside the string.

PHP Code example

$myVarilable = "output text";
echo "I'm ready to $myVarilable";
echo "I'm ready to ".$myVarilable;

PHP output display

I'm ready to output text

Its important to note that when using a concatenating syntax like period this will not perform math functions. In order to add to integers you can use the plus sign in either variable or your final echo output.

PHP Code example

$anumber = 10;
$bnumber = 5;
echo $anumber + $bnumber;

PHP output display

15

For more advanced concatenation you can use the short syntax format

PHP Code example

$anumber = 'textone';
$anumber .= 'texttwo';

PHP output display

textonetexttwo