PHP print Construct

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, print outputs a string. It is a language construct, not a normal function.

Because of that, you can write it without parentheses. It always returns 1.

Syntax

Call print with the values you need. Start with this line.

PHP
print "Hello, CodeToFun!";
CallWhat it does
print "Hello"Outputs Hello.
print("Hello")Works the same. Parentheses are optional.
$r = print "Hi"Outputs Hi and stores 1 in $r.

print takes only one argument. If you want several values separated by commas, use echo instead.

Library declarations
PHP
print(string $arg): int

Parameters

ParameterDescription
argRequired. The value to output. It is converted to a string.

Return Value

Return ValueDescription
intAlways 1.

PHP Examples

Example 1 — Print a Message

The simplest use: show text on the screen.

PHP
<?php
print "Hello, CodeToFun!" . PHP_EOL;
print("Parentheses work too" . PHP_EOL);

How It Works

  1. Write print and a string. No parentheses are needed.
  2. Add PHP_EOL. It ends the line.
  3. Try parentheses. They are allowed but not required.
  4. Both lines display. The output is the same either way.

Example 2 — print Always Returns 1

Because print returns a value, you can use it inside an expression.

PHP
<?php
$result = print "Hi" . PHP_EOL;

echo $result, PHP_EOL;

if (print "Checking..." . PHP_EOL) {
    echo "print counts as true", PHP_EOL;
}

How It Works

  1. Print and capture. The text appears first.
  2. The value is 1. echo shows it on the next line.
  3. Use it in a condition. 1 counts as true.
  4. Rarely needed. This is a neat fact, not a daily trick.

Example 3 — print vs echo

echo accepts commas. print takes a single argument, so join values with a dot.

PHP
<?php
echo "A", "B", PHP_EOL;
print "C" . "D" . PHP_EOL;

How It Works

  1. Use echo with commas. Each value is output in order.
  2. Use print with a dot. The dot joins the strings first.
  3. Same result for the eye. Both lines print on one row each.
  4. Pick echo for most work. It is a little more flexible.

Frequently Asked Questions

print outputs a string to the page or console. It is a language construct and always returns 1.
No. print "Hello" and print("Hello") both work.
echo can take several arguments separated by commas and returns nothing. print takes one argument and returns 1, so it can be used inside expressions.
The difference is so small it does not matter in real programs. Choose whichever reads better.
print cannot display arrays directly. Use print_r() or var_dump() instead.

Next: printf()

printf() prints a formatted string using placeholders like %s and %d.

printf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful