0
0
PHPprogramming~15 mins

Echo vs print behavior in PHP - Hands-On Comparison

Choose your learning style9 modes available
Echo vs print behavior
📖 Scenario: You are learning how to display text in PHP. Two common ways are using echo and print. They look similar but have small differences.
🎯 Goal: You will create a PHP script that shows how echo and print work by displaying messages and checking their return values.
📋 What You'll Learn
Create a variable with a string message
Use echo to display the message
Use print to display the message and capture its return value
Display the return value of print
💡 Why This Matters
🌍 Real World
Displaying messages and debugging output is a basic task in PHP web development.
💼 Career
Understanding how to output text and the differences between <code>echo</code> and <code>print</code> helps in writing clear and efficient PHP code.
Progress0 / 4 steps
1
Create a message variable
Create a variable called $message and set it to the string 'Hello, PHP!'.
PHP
Need a hint?

Use $message = 'Hello, PHP!'; to create the variable.

2
Display the message using echo
Use echo to display the variable $message.
PHP
Need a hint?

Write echo $message; to show the message.

3
Display the message using print and save return value
Use print to display the variable $message and save the return value in a variable called $result.
PHP
Need a hint?

Write $result = print $message; to display and save the return value.

4
Display the return value of print
Use echo to display the text 'Return value of print: ' followed by the value of $result.
PHP
Need a hint?

Use echo "Return value of print: $result"; to show the return value.