PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP VariablesPHP ConstantsPHP OutputPHP Data TypesPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms Finishing

PHP Output

PHP is executed on the server, only an output is sent to the browser.

Learning how to output data with PHP is really important!

In PHP, there are two output statements in PHP.

  • echo
  • print

Echo vs Print

echo print
Accepts multiple parameters Only accepts one parameter
Returns nothing Returns 1
Marginally faster than print Little bit slower than echo
Completely behaves as a language constructor Behaves like a function in many cases

PHP Echo Statement

The echo statement can be used to output text, variables, html markup, javascript code, and any other kind of text. Also, echo statement can output values of PHP variables and constants (We already did that in previous examples).

PHP Echo Example - Output Text

echo "<h1>I love PHP</h1>"; // html markup
echo "I am learning PHP at Hyvor Developer <br>";
echo "PHP: " . "Hypertext Preprocesser" . "<br>"; // connected with concatenation operator
echo "PHP: " , "the best language", " ever"; // connected with multiple parameters

// javascript
echo "<script>alert('Hello');</script>";

Run Example ››

echo can output variables.

PHP Echo Example - Output Variables

$hello = 'Hello World';
echo $hello; // outputs Hello World

echo "<br>";

$x = 5;
$y = 10;
echo $x + $y; // outputs 15

echo "<br>";

$link = '';
echo "<a href=$link>Hyvor Developer</a>";

Run Example ››

PHP Print Statement

The print statement can be used in the same way as echo.

PHP Print Example - Output Text

print "<h1>I love PHP</h1>"; // html markup
print "I am learning PHP at Hyvor Developer <br>";
print "PHP: " . "Hypertext Preprocesser" . "<br>"; // connected with concatenation operator
print "PHP: the best language ever"; // cannot have multiple parameters

// javascript
print "<script>alert('Hello');</script>";

Run Example ››

print can output variables.

PHP Print Example - Output Variables

$hello = 'Hello World';
print $hello; // outputs Hello World

print "<br>";

$x = 5;
$y = 10;
print $x + $y; // outputs 15

print "<br>";

$link = '';
print "<a href=\"$link\">Hyvor Developer</a>";

Run Example ››
Both echo and print can be used with or without parentheses as following.
  • echo or echo()
  • print or print()

PHP Single vs. Double Quotation Marks

In PHP it's important to understand how single quotation marks differ from double quotation marks. There is a key difference between the two types of quotation marks.

' "
Will be treated literally, displays exactly what you type Will be interpreted
Does not work with escape sequences, except \' and \\ Works with escape sequences
Variables inside are not parsed Variables inside are parsed

PHP Single vs Double Quotes Example

// both works
echo "Hello <br>";
echo 'Hello <br>';

// escape sequences are not parsed in single quotes
echo "\"World\" <br>"; // outputs "World"
echo '\"World\" <br>'; // outputs \"World\"

// variables are not parsed in single quotes
$hello = "Hello";
echo "$hello World <br>";
echo '$hello World <br>';

Run Example ››