PHP Output Methods

Output methods are used to display the data or output on screen. Various methods are provided by PHP for the same. These methods and their usage is as described below:

  1. print method
  2. echo method
  3. printf method
  4. sprintf method
  5. var_dump method
  6. print_r method

1.  PRINT : Print statement is used to display output on screen. It can be used with or without braces like print or print(). This method returns a bool value TRUE on call and can accept only one argument.

EXAMPLE 8 : USING PRINT STATEMENT TO DISPLAY TEXT AND VARIABLES
<?php
print “<h2>Welcome to the PHP World</h2>”;
$a = 10;
$b = 30;
$sum = $a + $b;
print “Sum of variables is -> ”.$sum;
?>

OUTPUT

Welcome to the PHP World

Sum of variables is  -> 40

2. ECHO : Echo is the most commonly used statement or method for printing in PHP. It can be used withor without braces like echo or echo(). This method of printing is faster because it does not return any value. An example to display text and variables using echo statement.It can also accept multiple parameters as its input.

EXAMPLE 8 : USING ECHO STATEMENT TO DISPLAY TEXT AND VARIABLES
<?php
echo “<h2>Welcome to the PHP World</h2>”;
echo “Accepting”,” multiple  ”, “parameters. <br> ”;  // not possible with print 
$a = 10;
$b = 30;
$sum = $a + $b;
echo “Sum of variables is -> ”.$sum;
?>

OUTPUT

Welcome to the PHP World

Accepting multiple parameters.

Sum of variables is  -> 40

3. PRINTF : This method of output uses format specifiers in order to display output on screen. It is also used in C/C++ language.

EXAMPLE 9 : USING PRINTF METHOD

<?php
$name = “Sehaj”;
$marks = 99;
printf(“%s has scored %d marks”, $name, $marks);
?>

OUTPUT

                  Sehaj has scored 99 marks.

4. SPRINTF : sprintf writes the formatted string to a variable.Type handling is also defined as under:

TypeSpecifiers
Strings
Integerd,u,c,o,x,X,b
Doubleg,G,e,E,f,F
EXAMPLE 10 : USING SPRINTF METHOD
<?php
$name = “Sehaj”;
$marks = 99;
$data = sprintf(“%s has scored %d marks”, $name, $marks);
echo $data;
?>

OUTPUT

                  Sehaj has scored 99 marks.

5. VAR_DUMP : var_dump method in PHP is used to dump information about one or more variables. This structured information includes its type and value. Arrays and objects are also explored in order to show their structure.

Syntax is :

var_dump(variable1, variable2, variable3,….);

EXAMPLE 11 : USING VAR_DUMP METHOD
<?php
	$array = array(“mon”, “tue”, “wed”, array(20, 40, 60));
var_dump($array);
?>

OUTPUT

                  array(4) {

                                    [0] =>

                                    string(1) “mon”

                                    [1] =>

                                    string(1) “tue”

                                    [2] =>

                                    string(1) “wed”

                                    [3] =>

                                    array(3){

                                                [0] => int (20)

                                                [1] => int(40)

                                                [2] => int(60)

}

}

6. PRINT_R : print_r method is used to display information about a variable in a more human readable form. Syntax is :

                        print_r(variable, return);

ParameterDescription
variableThe variable to return information about.
returnIf set true, the function will not print the information and only returns. Default is false
EXAMPLE 12 : USING PRINT_R METHOD
<?php
	$array = array(“mon”, “tue”, “wed”, array(20, 40, 60));
print_r($array);
?>

OUTPUT

Array([0] => “mon” [1] => “tue” [2] => “wed” [3] => Array([0] => 20 [1] => 40 [2] => 60))