Can we print multiple variables in a single echo statement in PHP?
Yes. You can print many variables in one echo statement. There are three common ways: separate them with a comma, join them with the dot (.) operator, or put them inside double quotes.
<?php
$name = "Robin";
$city = "Delhi";
echo $name, " lives in ", $city, "<br>"; // comma
echo $name . " lives in " . $city . "<br>"; // dot operator
echo "$name lives in $city"; // double quotes
?>
Output:
Tutorials Class - Output Window
Robin lives in Delhi
Robin lives in Delhi
Robin lives in Delhi
Variables are replaced by their values only inside double quotes. Single quotes print the text as it is.
Learn more about the similar topics:
| Tutorials |
|---|
| PHP Variables |
| PHP Data Types |
| PHP Constants |
| PHP isset() Function |
| PHP empty() Function |
| PHP is_null() Function |
| Exercises & Assignments |
|---|
| Write a program to print “Hello PHP” using variable |
| Write a program to print a string using echo+variable. |
| Write a program to print two variables in single echo |