In PHP there are various types of print statements by the use of language constructs and functions. These statements are varied based on the arguments they have or by the way of printing text to the browser. The following points deal with the different methods in showing output from PHP program.
- echo() – This is the most widely used output statement in PHP. Using this statement we can print one or more string. We can use this statement to have a string to print which may or may not enclosed with brackets. But, if we need to print multiple string that are comma separated, they shouldn’t be enclosed with brackets. Following code will explain what is possible and what is not in the usage of echo statement.
echo "Apple"; //(or) echo("Apple"); // Output AppleOrangeGrapes echo "Apple","Orange","Grapes"; // not a valid statement echo ("Apple","Orange","Grapes");
- print() – Unlike echo(), this function will print only one value to the browser. For example,
print "Apple"; //(or) print("Apple");
- printf(string_format,values) – This method is used to replace the values to the formated template. So, string format and values are passed as arguments of this function. For example,
printf('We are expected to score above %d%% for distinction', 85); //Output: We are expected to score above 85% //for distinction
This method may know this statement, if you are familiar with C programming. - sprintf() – This method is also same as printf() but the difference is that, it can return the formated string instead of printing it to the browser. Then we can store it into variable or database. For example,
- print_r() – This statement is used to print the string array or object array with an easy readable structure.
- var_dump() – var_dump() also prints array data in structured manner. But we can expect this function to provide additional information regarding the data, like, type of the data, length of the array and so on. For example, var_dump(false) prints bool(false) whereas print_r(false) returns empty string.
0 comments:
Post a Comment