Saturday, January 5, 2013

Types of Print Function in PHP


Introduction
PHP is a server side scripting language for creating dynamic web pages. There are so many functions available for displaying output in PHP. In this article I will explain some basic functions for displaying output in PHP. The basic functions for displaying output in PHP are as follows:
  • Print() Function
  • Echo() Function
  • Printf() Function
  • Sprintf() Function
  • Var_dump() Function
  • Print_r() Function
Print() Function 
Using this function we can display the outputs in the browser. This function returns the Boolean value true. We cannot print the multiple statements using this function. The print function plays the same role as the echo function.
Example1

<
body bgcolor="pink">print "Welcome Vineet Kumar Saini !!";?></body>
 Output
In the above example we display a simple message using the print() function.
1st.jpg
Example2
<html>
<
body bgcolor="pink">$rval="Welcome Vineet Kumar Saini is MCA Qualified !!";print $rval;?></body>
</
html>
Output
In the above example we display the message using a variable and the print() function.
2nd.jpg
Example3

<
body bgcolor="pink">print "Welcome Vineet Kumar Saini is "," ","MCA Qualified !!";?></body>
Output
In the above example we printed two strings using commas but the print() function doesn't display this message.
3rd.jpg
Echo() Function
The echo() function outputs one or more strings. Using this function we can display multiple statements in the browser. The echo() function is slightly faster than print(). Because it won't return a value.
Example1

<
body bgcolor="pink">echo "Welcome Vineet Kumar Saini !!";?></body>
Output
In the above example we display a simple message using the echo() function.
4th.jpg
Example2

<
body bgcolor="pink">echo "Welcome Vineet Kumar Saini is "," ","MCA Qualified !!";?></body>
Output
In the above example we print two strings using commas through the echo() function but the print() function doesn't display this message. It is the main difference between the echo() and print() functions.
5th.jpg
Printf() Function
The printf() function is also used in C, C++. The printf() function outputs a formatted string. Using this function we can display the output with the help of the formats specified.
Example
<html>
<
body bgcolor="pink">$name="Vineet Saini";
$age=24;
printf(
"The age of %s is %d years.",$name,$age);?></body></ html>
Output
In the above example we display the value of two variables i.e. string and integer using the printf() function.
6th.jpg
sprintf() Function
The sprintf() function writes a formatted string to a variable. This is the same as printf, but instead of displaying the output on the web page, it returns that output. sprintf() prints the result to a string.
Example
<html>
<
body bgcolor="pink">$name="Vineet Saini";
$age=24;
$rv=sprintf (
"The age of %s is %d years.",$name, $age);echo $rv;?></body></ html>
Output
7th.jpg
var_dump() Function
The var_dump() function displays information of a variable that includes its type and value. This function displays the variable value along with the variable data type. The var_dump() function is used to display structured information (type and value) about one or more variables.
Example

<
body bgcolor="pink">$name="Vineet Saini";
$age=24;
var_dump($name);
var_dump($age);
?></body>
Output
In the above example we declare two variables i.e. one is string and second is integer variable. The var_dump() function shows the types of these variables.
8th.jpg
print_r() Function
The Print_r() PHP function is used to return an array in a human readable form. This function displays the elements of an array and properties of an object. The print_r() function displays human-readable information about a variable.
Example

<
body bgcolor="pink">$arr=array ("Vineet","Kumar","Saini");
$arr1=
array(10,20,30);
print_r($arr);
print_r($arr1);
?></body>
Output
In the above example we display the element of an array using print_r() function.
9th.jpg
Conclusion
So in this article you saw various types of print functions. Using this article one can easily understand print functions in PHP.

No comments:

Post a Comment