Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. echo / print / printf()

echo / print / printf() Since: PHP 4(2000)

The basic syntax and functions for outputting values in PHP. Used in all kinds of situations, from debugging to generating HTML.

Syntax

// Outputs one or more values. Returns nothing.
echo value1, value2, ...;

// Outputs one value. Always returns 1.
print value;

// Formats and outputs directly using a format string.
printf(format_string, value1, value2, ...);

// Formats and returns a string using a format string.
sprintf(format_string, value1, value2, ...);

Syntax Overview

Syntax / FunctionDescription
echoA language construct that outputs one or more values. It is not a function, so parentheses are not required (though they are allowed). It has no return value and is the fastest output method.
printA language construct that outputs one value. It always returns 1, so it can be used inside expressions.
printf($format, ...)Formats values according to a format string and outputs them directly. Returns the length of the output string.
sprintf($format, ...)Formats values according to a format string and returns the result as a string. Nothing is output.

Common Format Specifiers

SpecifierDescription
%sOutputs as a string.
%dOutputs as an integer.
%fOutputs as a floating-point number. You can specify decimal places, e.g., %.2f.
%05dOutputs an integer zero-padded to 5 digits.
%%Outputs a literal % character.

Sample Code

<?php
// Output a string with echo.
echo "Hello, World!"; // Outputs: Hello, World!

// echo can output multiple values separated by commas.
echo "Name: ", "Taro", " Age: ", 25; // Outputs them concatenated.

// print outputs one value and always returns 1.
$result = print "PHP\n";
echo $result; // Outputs: 1

// Variable interpolation works inside double quotes.
$lang = "PHP";
echo "Language: $lang"; // Outputs: Language: PHP
echo 'Language: $lang'; // Single quotes do not interpolate variables.

// Use printf() to output with a format string.
$price = 1980;
$tax = 0.1;
printf("Price with tax: ¥%s", number_format($price * (1 + $tax))); // Outputs: Price with tax: ¥2,178

// sprintf() returns a formatted string without outputting it.
$formatted = sprintf("%s scored %d points.", "Tanaka", 85);
echo $formatted; // Outputs: Tanaka scored 85 points.

// Examples of zero-padding and decimal place control.
echo sprintf("Order number: %05d", 42); // Outputs: Order number: 00042
echo sprintf("Discount rate: %.1f%%", 12.5); // Outputs: Discount rate: 12.5%

// Example of formatting a date.
echo sprintf("%04d-%02d-%02d", 2026, 3, 4); // Outputs: 2026-03-04

Notes

echo is the most commonly used output method in PHP. Because it is a language construct rather than a function, it has no function-call overhead. Outputting multiple values with comma separation is also slightly faster than using the concatenation operator .. The only difference between echo and print is that print returns the value 1. Use echo unless you have a specific reason to use print.

printf() and sprintf() are formatting functions derived from C. They are useful when you need precise formatting, such as zero-padding numbers or controlling decimal places. printf() outputs directly, while sprintf() returns the string — use sprintf() when you need to store the result in a variable or pass it to another function.

For debugging variable output, see var_dump(). For output buffering, see ob_start().

If you find any errors or copyright issues, please .