ZetCode

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.

basic_print.php
<?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.

print_variable.php
<?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.

print_parentheses.php
<?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.

print_return.php
<?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.

print_html.php
<?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.

print_multiple.php
<?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.

print_vs_echo.php
<?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

Source

PHP print Documentation

This tutorial covered PHP print statement with practical examples showing basic usage, variable output, HTML printing, and comparisons with echo.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP basics tutorials.