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.
<?php
print "Hello World";
?>
- Browser Output: Hello World
Printing plain text string statements:
<?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:
<?php
$name = "Alex";
print $name;
?>
Text + Variable Together:
<?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:
<?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:
<?php
print("Hello PHP");
?>
Line Breaks with HTML:
Newlines require standard HTML <br> breaks:
<?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:
echoaccepts multiple comma-separated arguments.printtakes only one argument. - Return Value:
printalways returns1, allowing usage in conditional statements.echoreturns nothing. - Performance:
echois marginally faster because it skips computing a return value.
Code Syntax Comparison:
<?php
echo "PHP", " is ", "easy"; // ✅ Valid syntax
print "PHP", " is ", "easy"; // ❌ Syntax Error
?>
-
Practical Tip: In over 90% of real-world PHP applications,
echois preferred overprintdue to its cleaner syntax for multiple values and slight speed advantage.
-
📝 Brief Summary
-
printoutputs data to the web browser screen. -
Accepts only a single parameter and always returns the integer value
1. -
Slightly slower than
echo, makingechothe primary choice for standard project workflows.
