Various Methods For Outputting Data on the Browser in PHP

In this article we will see various methods for outputting data to the browser.

PHP provides the following methods for printing data in the browser:

  1. print()
  2. echo()
  3. printf()
  4. sprintf()

print(): this statement simply outputs to the browser. It's prototype looks as in:

int print(argument)

There are different typography for print() as in the following:

  1. <?php print("Be a part of c-sharpcorner");?>
  2. <?php
  1. $var="Be a part of c-sharpcorner";  
  2. print "$var";   
  3. ?>  

Both of the preceding statements provide the same output, which is:

Be a part of c-sharpcorner

Technically it is not a function, it's a language construct. It always returns 1 regardless of the output.

echo(): This is an alternative option to the print() statement. It just outputs the data to the browser without returning anything, like print(), and that is the main difference between print() and echo().

echo() is capable of outputting multiple strings.

It's prototype can be written in the following manner:

  1. void echo(string argument1,string argument2,….,string argument n);  

Example

  1. <?php   
  2. echo “Be a part of c-sharpcorner” ;   
  3. ?>  

Example

  1. <?php   
  2. $var1="hello";  
  3. $var2="how are ";  
  4. $var3="you? ";  
  5. echo $var1" Mr. "$var2,$var3;   
  6. ?>  

The output is:

Hello Mr . how are you?

Note: Here the question develops of which is faster, echo() or print()?

The answer is echo() is faster than print() because print returns a value whereas echo doesn't. This difference can't be seen because we can't examine such a small speed difference.

printf(): If we want to get output that is a combination of both text and dynamic information, in other words a variable.
It automatically separates both the static data and dynamic content.

It is useful because it provides user control on the variable information that will be displayed in the terms of type, precision, alignment and position.

It provides the extensibility as we used in C programming.

The number of type specifiers should be equal to or less than the number of their respective values.

It's prototype looks as in:

  1. integer printf(string format [, mixed arguments])  

Example

  1. <?php printf("Hello I got %d marks in maths and %d marks in hindi and got %.2f percentage in 10th class",97,86,85);?>  

The output will be:

Hello I got 97 marks in maths and 86 marks in hindi and got 85.00. percentage in 10th class.

Table for common type specifiers:
 
Type Description
%c Argument considered an integer; presented as a character corresponding to that ASCII value
%d Argument considered an integer; presented as a signed decimal number
%f Argument considered a floating-point number; presented as a floating-point number
%o Argument considered an integer; presented as an octal number
%s Argument considered a string; presented as a string
%u Argument considered an integer; presented as an unsigned decimal number
%x Argument considered an integer; presented as a lowercase hexadecimal number
%X Argument considered an integer; presented as an uppercase hexadecimal number
%b Argument considered an integer; presented as a binary number

sprint(): It's similar to printf() except that In printf() we directly get the output whereas sprint provides the output as a string. It's prototype can be written as:

 

  1. string sprintf(string format , mixed arguments);  

 

Example

  1. <?php   
  2. $he=sprintf("%d",23.76);  
  3. echo "$he $he";  
  4. ?>  

The output will be:

23 23

Thank you for your patience and reading this article.