Employablity presentation and Future Career Plan.pptx
Print function in PHP
1. 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
<html>
<body bgcolor="pink">
<?php
print "Welcome Vineet Kumar Saini !!";
?>
</body>
</html>
Output
In the above example we display a simple message using the print() function.
www.vineetsaini.wordpress.com
2. Example2
<html>
<body bgcolor="pink">
<?php
$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.
Example3
<html>
<body bgcolor="pink">
<?php
print "Welcome Vineet Kumar Saini is "," ","MCA Qualified !!";
?>
</body>
</html>
Output
In the above example we printed two strings using commas but the print() function doesn't
display this message.
www.vineetsaini.wordpress.com
3. 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
<html>
<body bgcolor="pink">
<?php
echo "Welcome Vineet Kumar Saini !!";
?>
</body>
</html>
Output
In the above example we display a simple message using the echo() function.
Example2
<html>
<body bgcolor="pink">
www.vineetsaini.wordpress.com
4. <?php
echo "Welcome Vineet Kumar Saini is "," ","MCA Qualified !!";
?>
</body>
</html>
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.
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">
<?php
$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.
www.vineetsaini.wordpress.com
5. 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">
<?php
$name="Vineet Saini";
$age=24;
$rv=sprintf ("The age of %s is %d years.",$name, $age);
echo $rv;
?>
</body>
</ html>
Output
var_dump() Function
www.vineetsaini.wordpress.com
6. 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
<html>
<body bgcolor="pink">
<?php
$name="Vineet Saini";
$age=24;
var_dump($name);
var_dump($age);
?>
</body>
</html>
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.
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
<html>
<body bgcolor="pink">
<?php
$arr=array ("Vineet","Kumar","Saini");
$arr1=array(10,20,30);
print_r($arr);
print_r($arr1);
www.vineetsaini.wordpress.com
7. ?>
</body>
</html>
Output
In the above example we display the element of an array using print_r() function.
Conclusion
So in this article you saw various types of print functions. Using this article one can easily
understand print functions in PHP.
www.vineetsaini.wordpress.com