Write a program to print two variables in single echo

Description: Write a PHP program to print two variables using only one echo statement.

Rules and hints:

  • The first variable has the text “Good Morning.”
  • The second variable has the text “Have a nice day!”
  • The output must be: Good Morning. Have a nice day!
  • You can use only one echo statement.

PHP program to print two variables in a single echo

<?php
$first = "Good Morning.";
$second = "Have a nice day!";

echo $first . " " . $second;
?>

Output:

Tutorials Class - Output Window

Good Morning. Have a nice day!

How it works

  • The dot operator joins the two variables, and a space in quotes is added between them.
  • Another way is the comma: echo $first, " ", $second;.

See three ways to print many variables in one echo.


Learn more about the similar topics:
Tutorials
PHP Variables
Exercises & Assignments
Write a program to print “Hello World” using echo
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
Interview Questions & Answers
How to check if a variable is empty in PHP?
Is PHP a case sensitive language?
What are the different data types in PHP?
What is the difference between $var and $$var in PHP?
Which special characters are allowed in PHP variable names?
Can we print multiple variables in a single echo statement in PHP?
How to check the data type of a variable in PHP?