Print function in PHP


Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Print function in PHP

  1. 1. Print Function in PHPIntroductionPHP is a server side scripting language for creating dynamic web pages. There are so manyfunctions available for displaying output in PHP. In this article I will explain some basicfunctions for displaying output in PHP. The basic functions for displaying output in PHP areas follows:  Print() Function  Echo() Function  Printf() Function  Sprintf() Function  Var_dump() Function  Print_r() FunctionPrint() FunctionUsing this function we can display the outputs in the browser. This function returns theBoolean value true. We cannot print the multiple statements using this function. The printfunction plays the same role as the echo function.Example1<html><body bgcolor="pink"><?phpprint "Welcome Vineet Kumar Saini !!";?></body></html>OutputIn the above example we display a simple message using the print() function.
  2. 2. Example2<html><body bgcolor="pink"><?php$rval="Welcome Vineet Kumar Saini is MCA Qualified !!";print $rval;?></body></html>OutputIn the above example we display the message using a variable and the print() function.Example3<html><body bgcolor="pink"><?phpprint "Welcome Vineet Kumar Saini is "," ","MCA Qualified !!";?></body></html>OutputIn the above example we printed two strings using commas but the print() function doesntdisplay this message.
  3. 3. Echo() FunctionThe echo() function outputs one or more strings. Using this function we can display multiplestatements in the browser. The echo() function is slightly faster than print(). Because itwont return a value.Example1<html><body bgcolor="pink"><?phpecho "Welcome Vineet Kumar Saini !!";?></body></html>OutputIn the above example we display a simple message using the echo() function.Example2<html><body bgcolor="pink">
  4. 4. <?phpecho "Welcome Vineet Kumar Saini is "," ","MCA Qualified !!";?></body></html>OutputIn the above example we print two strings using commas through the echo() function butthe print() function doesnt display this message. It is the main difference between theecho() and print() functions.Printf() FunctionThe 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>OutputIn the above example we display the value of two variables i.e. string and integer using theprintf() function.
  5. 5. sprintf() FunctionThe sprintf() function writes a formatted string to a variable. This is the same as printf, butinstead of displaying the output on the web page, it returns that output. sprintf() prints theresult 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>Outputvar_dump() Function
  6. 6. The var_dump() function displays information of a variable that includes its type andvalue. This function displays the variable value along with the variable data type. Thevar_dump() function is used to display structured information (type and value) about one ormore variables.Example<html><body bgcolor="pink"><?php$name="Vineet Saini";$age=24;var_dump($name);var_dump($age);?></body></html>OutputIn the above example we declare two variables i.e. one is string and second is integervariable. The var_dump() function shows the types of these variables.print_r() FunctionThe Print_r() PHP function is used to return an array in a human readable form. Thisfunction 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);
  7. 7. ?></body></html>OutputIn the above example we display the element of an array using print_r() function.ConclusionSo in this article you saw various types of print functions. Using this article one can easilyunderstand print functions in PHP.