DONATE Hyvor Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP OutputPHP Data TypesPHP Type CastingPHP 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

Also, there's a shorthand syntax for the echo function. (Since PHP 5.4)

  • <?= ?>

Echo vs Print

echo print
Accepts multiple parameters Accepts only 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


<?php
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


<?php
$hello = 'Hello World';
echo $hello; // outputs Hello World

echo "<br>";

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

echo "<br>";

$link = 'https://developer.hyvor.com';
echo "<a href=$link>Hyvor Developer</a>";

Run Example ››

PHP Print Statement

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

PHP Print Example - Output Text


<?php
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


<?php
$hello = 'Hello World';
print $hello; // outputs Hello World

print "<br>";

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

print "<br>";

$link = 'https://developer.hyvor.com';
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 Echo and Print with Parentheses


<?php
echo 'Hello';
echo('Hello');

print 'Hello';
print('Hello');

Run Example ››

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


<?php
// 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>"; // outputs Hello World
echo '$hello World <br>'; // outputs $hello World

Run Example ››

Shorthand Echo

Developers use this shorthand echo syntax when embedding HTML with PHP. Of course, this can be useful in many cases.

PHP Shorthand Echo Example


<h1><?= 'Hyvor Developer' ?></h1>

Run Example ››

This PHP code is a special one in PHP, as it uses a different opening tag. (<?=). Any string output wrapped with <?= and ?> will be echoed. See the following example.

PHP Shorthand Echo Example 2


<?php
$name = 'Hyvor Developer'; ?>

<h1><?= $name ?></h1>
<!-- same as -->
<h1><?php echo $name ?></h1>

<!-- More examples -->
<h2><?= $name . ' Blog' ?></h2>
<h2><?= $name . ' Blog' // comments are allowed ?></h2>

Run Example ››
0
286