PHP

🖨️ What Is PHP print?

In PHP, the print statement is used to output text, variables, or dynamic HTML markup to the web browser screen. While functionality-wise it is very similar to echo, print has a return value of 1, allowing it to be used in expressions.

  • In Short: print = Display / Output content to the browser screen (returns 1).

1️⃣ Basic Usage & Printing Strings

Like echo, string content must be wrapped inside double or single quotation marks.

CSS

<?php
print "Hello World";
?>

  • Browser Output: Hello World

Printing plain text string statements:

CSS

<?php
print "I am learning PHP";
?>

2️⃣ Printing Variables & Concatenation

Variables stored in memory can be rendered directly or concatenated using the dot (.) string joining operator:

Printing a Variable:

CSS

<?php
$name = "Alex";
print $name;
?>

Text + Variable Together:

CSS

<?php
$name = "Alex";
print "Hello " . $name;
?>

  • Browser Output: Hello Muhammet

3️⃣ Outputting HTML & Parentheses Usage

You can generate standard HTML tags dynamically inside print strings:

CSS

<?php
print "<h1>Title</h1>";
print "<p>This is a paragraph</p>";
?>

Although print is a language construct rather than a standard function, optional functional syntax with parentheses is also valid:

CSS

<?php
print("Hello PHP");
?>

Line Breaks with HTML:
Newlines require standard HTML <br> breaks:

CSS

<?php
print "First line<br>";
print "Second line";
?>

⚖️ Differences Between echo and print

While both constructs seem nearly identical, their technical behavior differs significantly:

  • Multiple Parameters: echo accepts multiple comma-separated arguments. print takes only one argument.
  • Return Value: print always returns 1, allowing usage in conditional statements. echo returns nothing.
  • Performance: echo is marginally faster because it skips computing a return value.

Code Syntax Comparison:

CSS

<?php
echo "PHP", " is ", "easy"; // ✅ Valid syntax
print "PHP", " is ", "easy"; // ❌ Syntax Error
?>

  • Practical Tip: In over 90% of real-world PHP applications, echo is preferred over print due to its cleaner syntax for multiple values and slight speed advantage.
  • 📝 Brief Summary

  • print outputs data to the web browser screen.
  • Accepts only a single parameter and always returns the integer value 1.
  • Slightly slower than echo, making echo the primary choice for standard project workflows.