PHP offers different ways to display output data to the browser.
- Echo Statement
- Print Statement
- Multi-line Printing
- Printf Function
- Sprintf Function
- Print_r Function
- Var_dump Function
- Vprintf Function
- Vsprintf Function
- Comparison Among Statements
It is most common and one of the display and output statement in PHP. It efficient than any other display statements.
Syntax:
echo expression1, expression2, ...;
Explanation:
- It prints one or more expressions.
- It doesn't return a value.
- It can be used without parentheses.
Example:
echo "Welcome, user! ", "to the page";
echo "<p>This is a paragraph.</p>";
It is most common and one of display and output statement in PHP.
Syntax:
print expression;
Explanation:
- It prints a single expression only.
- It returns 1 on success, 0 on failure.
- It requires parentheses sometimes.
Example:
print("Welcome, user!");
print "<p>This is a paragraph.</p>";
To print multiple lines, use either print
or echo
with newlines:
Example:
echo "Line 1\nLine 2\nLine 3";
Example: use nl2br()
to convert newlines to <br>
tags:
echo nl2br("Line 1\nLine 2\nLine 3");
The printf()
function in PHP is used to format and print strings. It accepts a format string as the first argument and additional arguments as the subsequent parameters. The format string contains format specifiers that act as a placeholder that are replaced with the corresponding arguments.
Syntax:
printf(format_string, argument1, argument2, ...);
Explanation:
-
It formats output using placeholders in the format string.
-
Format specifiers:
Specifier Description Example %s String printf("Name: %s", "Kumar")
%d Integer printf("Age: %d", 30)
%f Floating-point number printf("Pi: %.2f", 3.14159)
%e Scientific notation printf("Avogadro's number: %e", 6.02214076e23)
%x Hexadecimal number (lowercase) printf("Hexadecimal: %x", 255)
%X Hexadecimal number (uppercase) printf("Hexadecimal: %X", 255)
%o Octal number printf("Octal: %o", 255)
%b Binary number printf("Binary: %b", 255)
%% Literal percent sign printf("Percentage: %%")
-
Additional Formatting Options:
- Precision: Specify the number of decimal places for float numbers:
printf("Pi: %.3f", 3.14159); // Output: Pi: 3.142
- Zero-padding: Use the
0
flag:
printf("Visits: %010d", 30); // Output: Visits: 000000030
Example:
$name = "Kumar";
$age = 30;
printf("Name: %s, Age: %d\n", $name, $age);
$price = 36.89;
$hex = 0xFF;
$binary = 1010;
printf("Price: %.2f\nHex: %x\nBinary: %b\n", $price, $hex, $binary);
It similar to printf()
function, but returns the formatted string instead of printing it directly.
Syntax:
$formatted_string = sprintf(format_string, argument1, argument2, ...);
Example:
$name = "Kumar";
$age = 30;
$formatted_string = sprintf("Name: %s, Age: %d\n", $name, $age);
echo $formatted_string;
It prints human-readable information about a variable. For debugging and inspecting complex data structures.
Syntax:
print_r(expression, boolean);
Explanation:
- It accepts optional second argument
boolean
to controls the output format:true
: Returns the output as a string.false
(default): Prints the output directly.
Example:
$array = [1, 2, 3];
print_r($array);
echo print_r($array, true);
It prints detailed information about one or more expressions, including their type and value. And it is useful for debugging.
Syntax:
var_dump(expression1, expression2, ...);
Example:
$array = [1, 2, 3];
var_dump($array);
It is similar to printf
function, but uses an array of arguments instead of number of variable arguments.
It is similar to sprintf
function, but accepts array of arguments.
Function | Description | Return Value | Use Cases |
---|---|---|---|
echo |
Outputs one or more strings | None | Simple output, printing HTML content |
print |
Outputs a single string | 1 on success, 0 on failure | Similar to echo , but can be used in one expression at a time |
printf |
Formats output using placeholders | None | Formatting output with specific data types (e.g., numbers, strings) |
sprintf |
Similar to printf , but returns the formatted string instead of printing it |
Formatted string | Storing formatted strings for later use |
vprintf |
Like printf , but takes an array of arguments |
None | Formatting output with number of arguments as a variable |
vsprintf |
Like sprintf , but takes an array of arguments and returns the formatted string |
Formatted string | Storing formatted strings with a variable number of arguments |
print_r |
Prints human-readable information about a variable | None | Debugging and inspecting complex data structures (arrays, objects) |
var_dump |
Dumps information about a variable, including its type and value | None | Debugging and inspecting variables in detail |
Choosing the Right Function:
- Return Value:
print
returns 1 on success, whileecho
doesn't return anything. - Argument Count:
echo
can take multiple arguments, whileprint
takes only one. - Formatting:
printf
andsprintf
offer more control over formatting output using placeholders. - Debugging:
print_r
andvar_dump
are useful for debugging and inspecting complex data structures.