PHP Variables

Variables are used to store a value that can change during the program execution. PHP variables are part of Basic programming. Therefore, it is good to practice on these and understand basic PHP programming concepts.

Here, you will find Assignments & Exercises related to PHP Variables. These exercises list contains exercise description and instructions. You need to write a program as per instructions. Then you can match it with our solution.

All PHP exercises are available in the form of PHP program description, instruction and their solution. These PHP programs can also be given as an assignment for PHP students.

Write a program to print “Hello World” using echo

Description: Write a PHP program to print “Hello World” using echo only.

Rules and hints:

  • You can not use any variable.
  • End every statement with a semicolon.

PHP program to print Hello World

<?php
echo "Hello World";
?>

Output:

Tutorials Class - Output Window

Hello World

How it works

  • <?php and ?> are the PHP tags.
  • echo prints the text on the page.
  • The text is in double quotes, and the statement ends with ;.

Related: PHP Syntax and echo vs print.

Write a program to print “Hello PHP” using variable

Description: Write a PHP program to print “Hello PHP” using a variable.

Rules and hints:

  • You can not write the text directly in echo.
  • Store the text in a variable first, and then print the variable.

PHP program to print Hello PHP using a variable

<?php
$message = "Hello PHP";

echo $message;
?>

Output:

Tutorials Class - Output Window

Hello PHP

How it works

  • $message is a variable that stores the text.
  • echo $message; prints the value of the variable.

Related: PHP Variables.

Write a program to print a string using echo+variable.

Description: Write a PHP program to print “Welcome to the PHP World”. Keep a part of the text in a variable, and write the other part directly in echo.

Rules and hints:

  • Use a variable that contains the string “PHP World”.
  • Join the variable with the rest of the text using the dot (.) operator.

PHP program to print a text using echo and a variable

<?php
$world = "PHP World";

echo "Welcome to the " . $world;
?>

Output:

Tutorials Class - Output Window

Welcome to the PHP World

How it works

  • The dot operator joins the text with the variable.
  • You can also write echo "Welcome to the $world";. Variables inside double quotes are replaced by their values.

Related: How to concatenate two strings in PHP.

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.