1. php
  2. /basics
  3. /variables

Introduction to PHP variables

Definition

In PHP, variables are used to store and manipulate data. A variable is a container for a value, and it is defined by a name that begins with a dollar sign ($).

The basic syntax for declaring a variable in PHP is:

$variable_name = value;

For example:

$name = "John Doe";
$age = 30;
$is_student = false;

In the examples above, three variables are declared: $name, $age, and $is_student. The variable $name is assigned the string value "John Doe", the variable $age is assigned the integer value 30 and the variable $is_student is assigned the boolean value false.

PHP is a loosely typed language, which means that the type of a variable does not need to be specified when it is declared. PHP automatically assigns the correct data type based on the value that is assigned to the variable.

It's also possible to change the value of a variable by re-assigning a new value to it

$name = "Jane Smith"; // the value of $name is now "Jane Smith"

It's important to note that variable names in PHP are case-sensitive, which means that $name and $Name are two different variables.

Also, PHP has some predefined variables called superglobals, they are automatically available in all scopes and can be accessed from anywhere in the script, examples of superglobals are $_GET, $_POST, $_SERVER and $_SESSION

Examples

<?php

$name = "John Doe";
$age = 30;
$is_student = false;

echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
echo "Is a student: " . $is_student . "<br>";

$age = 35; // re-assign new value to the variable

echo "Updated age: " . $age . "<br>";

$name = "Jane Smith"; // re-assign new value to the variable

echo "Updated name: " . $name . "<br>";

?>

In this example, three variables are declared: $name, $age, and $is_student. Then the variables are printed out to the screen with the echo statement, the output will be:

Name: John Doe
Age: 30
Is a student: false

Then the values of the variables are re-assigned and printed out again, the output will be:

Updated age: 35
Updated name: Jane Smith

As you can see, the values of the variables can be changed and printed out multiple times.

Also, in the example above, it's possible to see that PHP automatically assigns the correct data type based on the value that is assigned to the variable, $name is a string, $age is an integer, and $is_student is a boolean.

Best Practices

  1. To make it easy to understand which variable is being used and for what purpose, it is important to use clear and descriptive variable names.

  2. Use a consistent naming convention throughout your code, such as camelCase or snake_case.

  3. Always initialize variables before using them, even if you know the value will be assigned later.

  4. Avoid using short variable names, as they can make code more difficult to read and understand.

  5. Avoid using abbreviations when naming variables, as they can make code more difficult to read and understand.

  6. Avoid using keywords, such as "function" or "class" as variable names.

  7. Use underscores to separate words in long variable names, to make them more readable.

  8. Be aware of the scope of variables and the impact that changes to them can have on the rest of the script.

  9. As much as possible, try to avoid using global variables and instead pass variables as function arguments or use object-oriented programming techniques to access shared data.

  10. Avoid creating unnecessary variables, as they can make code more difficult to understand and maintain.