PHP Print Statements
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 strings that are comma separated, they shouldn’t be enclosed in brackets. The following code will explain what is possible and what is not in the usage of echo statement.
// Output AppleOrangeGrapes
// not a valid statement
- print() – Unlike echo(), this function will print only one value to the browser. For example,
- printf(string_format, values) – This method is used to replace the values to the formatted template. So, string format and values are passed as arguments to this function. For example,
printf('We are expected to score above %d%% for distinction', 85);
//Output: We are expected to score above 85%
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 formatted 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 easily 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, the length of the array and so on. For example, var_dump(false) prints bool(false) whereas print_r(false) returns empty string.
This PHP code tutorial was published on April 18, 2013.
↑ Back to Top