PHP print Statement
last modified April 16, 2025
The PHP print
statement is a language construct used for outputting
data. It's similar to echo
but with some differences. Print is
fundamental for displaying content in PHP applications.
Basic Definitions
The print
statement outputs one or more strings. It's not actually
a function but a language construct. Parentheses are optional when using print.
Unlike echo
, print always returns 1, allowing it to be used in
expressions. It can output strings, variables, and HTML content directly.
Syntax: print "string";
or print("string");
. The
statement converts non-string values to strings automatically.
Basic print Usage
This example demonstrates the simplest form of print statement.
<?php declare(strict_types=1); print "Hello, World!";
The code outputs the string "Hello, World!" to the browser or console. The print statement doesn't require parentheses in this basic form. It's one of the most common ways to output text in PHP.
Printing Variables
This example shows how to print variable values with print.
<?php declare(strict_types=1); $name = "John Doe"; $age = 32; print "Name: $name, Age: $age";
The code demonstrates variable interpolation in strings with print. Variables are automatically converted to strings when printed. Double quotes allow variable values to be inserted directly.
Print with Parentheses
This example demonstrates using print with parentheses syntax.
<?php declare(strict_types=1); print("This is printed with parentheses.");
The parentheses syntax works identically to the standard form. This style is sometimes preferred for readability. The parentheses don't affect the output.
Print Return Value
This example shows how print returns 1 and can be used in expressions.
<?php declare(strict_types=1); $result = print "Hello"; echo "<br>Return value: $result";
The code assigns print's return value to a variable. Print always returns 1, unlike echo which has no return value. This allows print to be used where expressions are required.
Printing HTML
This example demonstrates printing HTML content directly.
<?php declare(strict_types=1); print "<h1>Welcome</h1>"; print "<p>This is a paragraph.</p>";
The code outputs HTML markup directly to the browser. Print can output any valid HTML content. The browser will render the HTML elements properly.
Print Multiple Values
This example shows how to print multiple values with concatenation.
<?php declare(strict_types=1); $firstName = "Jane"; $lastName = "Smith"; print "Full name: " . $firstName . " " . $lastName;
The code concatenates strings and variables with the dot operator. Unlike echo, print can only take one argument. Concatenation is needed for multiple values.
Print vs Echo
This example compares print and echo statements.
<?php declare(strict_types=1); // Echo can take multiple parameters echo "Hello", " ", "World", "!"; // Print can only take one parameter print "Hello World!";
The main difference is echo can output multiple strings separated by commas. Print is slightly slower as it returns a value. Both are language constructs, not functions.
Best Practices
- Readability: Use consistent style (with/without parentheses).
- Performance: Use echo for multiple outputs in one statement.
- Security: Escape output with htmlspecialchars when needed.
- Concatenation: Use proper spacing in concatenated strings.
- Formatting: Break long print statements for better readability.
Source
This tutorial covered PHP print statement with practical examples showing basic usage, variable output, HTML printing, and comparisons with echo.
Author
List all PHP basics tutorials.