How to use PHP variables


Variables are used to store and manipulate data in PHP. In this tutorial, we’ll learn how to create and use variables in PHP.


Creating a variable in PHP is simple. You first need to declare it with the “$” symbol and then give it a name. You can then assign a value to the variable using the “=” operator.

$variable_name = value;

Parameter Values

Parameter Description
variable_name The name you want to give the variable.
value The value you want to assign to the variable.

Technical Details

Variable names can only contain letters, numbers, and underscores. They cannot start with a number and are case sensitive. PHP automatically determines the data type of a variable based on the value assigned to it.

Usage examples

  $name = 'John';
  $age = 25;
  $is_active = true;
  echo 'My name is ' . $name . '. I am ' . $age . ' years old.';
  if($is_active) {
    echo 'I am active.';
  } else {
    echo 'I am not active.';

In the above example, we have created three variables – $name, $age, and $is_active. We have assigned the values ‘John’ (a string), 25 (an integer), and true (a boolean) to these variables respectively. We have then used these variables in an echo statement to output a message. We have also used the $is_active variable in an if-else statement to conditionally output a message based on its value.

For more information on PHP variables, you can check out the official PHP documentation.

  Written by: Steven Iversen     23-05-2023     Written in: PHP tutorials

One comment

  • Gianna says:

    I really enjoyed this tutorial on how to use PHP variables . It was very clear and easy to follow. I have been struggling to understand the concept of variables, but this tutorial explained it in a way that finally made sense to me. The examples were also very helpful in illustrating how variables work. Thank you for providing such valuable information!

Leave a Reply

Your email address will not be published. Required fields are marked *