PHP Variables

This video was created by Dani Krossing

PHP variables, like JavaScript Variables, are used to store values of different types, like strings, numbers, and booleans. Variables in PHP are loosely typed, which means you not need to set the data type when declaring the variable.

Naming Variables

In PHP, when defining a variable, variables must adhere to certain naming conventions. Below are some general rules when naming variables:

  • All variable names must start with a dollar sign ($)
  • The dollar sign must be followed by a letter or an underscore (_)
  • Variable names can contain letters, numbers, underscores, or dashes (-)
  • Variable names cannot contain spaces
  • Variable names are case-sensitive
  • Reserved words cannot be used as variable names

The following example is a list of valid and unique variable names. Each name will create a new variable.

<?php 
  // valid variable names
  $item = "Item 1";
  $Item = "Item 2"; 
  $ITEM = "Item 3";
  $myVariable = "My first variable";
  $my_variable = "My second variable";
  $my_variable3 = "My third variable";

  // valid, but discouraged
  $book-variable = 1;
  $_book = 2;
  $__book = 3;
?>

Naming conventions

While it is possible to create many different names for variables, there are best practice conventions for how variables should be named to avoid confusion and mistakes. Take for instance the example above, notice that the word 'item' is used three different times. Because variable names are case-sensitive, each variable is actually a different variable. But it could be confusing when we might one.

The following is a list of guidelines when naming variables.

  1. Be consistent. Choose a name convention and stick with it.
  2. Be descriptive. A variable name should describe the nature of the value of the variable
  3. Be concise. Long variable names are difficult to write and can lead to errors
  4. Avoid confusing characters. Characters like the dash (-) or underscore (_) can be easily misread.

Using PHP Variables

To retrieve the value stored inside of a PHP Variable, you simply call the variable name.

<?php
  // gives $var a value
  $var = 10;
  echo $var; // outputs 10

To change the value of a variable, call the variable name followed by an equal sign (=) and the desired value.

<?php
  // gives $var a value
  $var = 10;
  echo $var; // outputs 10
 
  echo "<br>";
  
  // gives the $var a new value
  $var = 100;
  echo $var; // outputs 100

Deleting a PHP Variable

If you ever want to delete a variable there is a function called unset() that will let you destroy a variable.

<?php 
  $var = 10;

  // deletes $var
  unset($var);
  echo $var; // notice stating $var is undefined

Constants

A constant is a variable that once defined can never be changed. In PHP, Constants can be defined using the const keyword or the define() function.

The const keyword is limited to scalar data types (booleans, numbers, and strings) and must be declared outside of any functions, loops, or conditional statements. The define() function allows a constant to be defined to any expression, may be defined anywhere in the code.

Other traits of Constants are:

  • Constants do not start with a $
  • Constants are not restricted by rules of Variable Scope
  • Constants may not be redefined or undefined once they are set
  • Constants are typically written in all-caps
const PI = 3.14;
define('COLORS', ['red', 'green', 'blue']);