PHP Echo and Print


In this lesson, you will learn about echo and print statements. We have already seen hello.php example in Lesson 1 when the echo statement is used to display a text string. PHP’s primary function is to output HTML. Almost every action PHP performs ends up with outputting HTML onto a webpage.

PHP Echo Statement

In PHP, echo is a language construct and not a function, so using parenthesis is optional here. Echo is used to output strings or variables.

The Syntax of the echo statement

echo (string $arg1 [,string $s … ]):void

This code will output all parenthesis. No additional newline is appended. Echo doesn’t behave like a function, so you can’t use in the context of a function. Echo statement also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. Most developers prefer echo over print to output HTML. Most PHP functions or programs end, echoing a string of HTML markup.

This will also <?= $print ?> variable.

Example of echo in PHP

<h4>This is an HTML string.</h4>

Output

Now, use the echo statement to print the string.

<h4><?php echo “This is an HTML string.”; ?></h4>

Output

If you check the source code of the page, you will see the following.

<h4>This is an HTML string.</h4>

Even, you have used PHP echo statement to output the HTML string in the second program; there was no difference in the output and HTML generated in the source code. When we tell PHP to echo string, it gets printed straight onto the web page as pure HTML.

Display Strings in PHP

We can use echo statement to output strings (sequence of characters, numbers, symbols ) to be displayed within single or double quotes.

Example of Display String

These are the few examples of echoing strings using the echo statement.

echo “Hi, this will print the string”;
echo ‘HI, this will print the string’;
echo “<br/> Hi, this will print the string on next line”;
echo “<h4>let’s print h4 heading</h4>”;
echo 2*7; // will print the string 14
echo true; // will print 1 as 1 is equivalent to Boolean value true.  

PHP is a loosely typed programming language which lets you convert data type on the fly. But don’t be careless just because it allows you.

Display Strings as multiple arguments in PHP

You can pass multiple strings to the statement instead of a single string, separated by comma operator. For example:

Example of display string as multiple arguments

<?php 
   echo “hello”, “world”;
?> 

Display variables in PHP

Printing variable’s values using the echo statement is as simple as using a plain string with echo. To print variables, merely using them variable names without quotations.

Example of display variable I

<?php 
$user_name = “John”;
$greetings = “Hello ”;
echo $greetings. $user_name.”!”;
?>

This code will print
Hello John!

As you can see, the above echo statement print two variables with a string “!”.

Example of display variable II

<?php

   $x = 5;
   $y = 7;

  echo "x+ y = ";
  echo $x + $y;

  ?>

This code will print

x+ y = 12

PHP print statement

Similar to the echo statement, the PHP print statement is another function to print output. It is also a language construct, so it is okay to use it with or without parenthesis: print or print().

The difference between the print and echo is that the print can have only 1 parameter at a time and thus print a single string. Also, echo statement doesn’t return anything whereas print statement returns a value 1. Like echo, it is used to print strings and variables.

Example of PHP print statement I

  <?php

       print "Hello world!";

  ?>

Example of PHP print statement II

<?php
     $x = 5;
     $y = 7;
     print "x + y = ";
     print $x+$y;  
  ?>

Other ways to output HTML in PHP

Echo and print are not the only way to output HTML content in PHP.

PHP function Var_dump

var_dump is a useful function that prints out information about the PHP data type. It is not designed for users to see, but rather to help developers debug their code.

PHP fucntion Print_r

print_r() is more like var_dump() . It will output more readable HTML and mostly used for array().

Example of print_r function

  <?php
    $arr = array(1,2,3,4,5,6);
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
  ?>
Array Output
PHP Variables Tutorial Home PHP Strings
Tutorials Panel

About Tutorials Panel

programming tutorial and source code for Software Engineers and developers. https://twitter.com/TutorialsPanel

View all posts by Tutorials Panel →