Grayscale profile picture

Patrique Ouimet

Senior Product Engineer

PHP Variable Basics

Fri, Nov 26, 2021 12:40 PM

PHP Variable Basics

Variables are used to temporarily hold data during code execution. In PHP, variables are declared with a leading dollar sign "$" symbol followed by the name you wish to assign it, then an equal "=" symbol, then the value to assign to the variable followed by a semi colon. Here's an example:

$a = 321;

To access the variable, use the "$" symbol followed by it's name, example:

echo $a;

This will output 321

TYPES

Variables in PHP have types but they are not declared in code (as seen in other languages such as Java; String a = "John") but inferred by the PHP interpreter. Take a look at the following example:

$a = 'John';

In the above example, the variable named "a" will be assigned a string value of "John" and will remain such until either the value is updated or deleted (unset).

There are many possible variable types in PHP some of which we call scalar: string, int, float, and bool. Others which are non-scalar: array, and object.

STRING

A string type contains a series of characters (https://www.php.net/manual/en/language.types.string.php). It can be declared in a few different manners, here are some examples:

// single quotes
$a = 'Hello world!';

In this example we see the usage of "single quote" format. The value which we are assigning is surround by single quotes. This format does not respect escape characters.

// double quotes
$greeting = 'Hello';
$a = "$greeting world!";

In this example we see the usage of "double quote" format. The value which we are assigning to the variable is surround by double quotes. You'll notice we've add a variable within the string we're assigning to the variable. This is additional functionality the double quote format provides where we can inject other variables into the string. It also allows us to input escape characters, a full list can be found here: https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double

// nowdoc
$a = <<<MYSTR
Hello
world!
MYSTR;

In this example we see the usage of "heredoc" format. The value which we assigning to the variable starts with 3 less than < symbols followed by a delimiter, then proceeded by the string we wish to assign the variable followed by the delimiter previously used. The delimiter can be anything you like. The heredoc format maintains the whitespace in the assigned string, which means that "Hello" and "world!" will be displayed on separate lines.

// heredoc
$greeting = 'Hello';
$a = <<<MYSTR
$greeting
world!
MYSTR;

In this example we see the usage of "heredoc" format. The value which we assigning to the variable starts with 3 less than < symbols followed by a delimiter, then proceeded by the string we wish to assign the variable followed by the delimiter previously used. The delimiter can be anything you like. The heredoc format maintains the whitespace in the assigned string, which means that "Hello" and "world!" will be displayed on separate lines. Equally this format allows us to inject variables into the assigning string, similarly to the double quote format.

INT

An integer type contains only whole numbers. Here's an example:

$a = 54321;

FLOAT

An float type contains floating point numbers. Here's an example:

$a = 54321.22;

BOOLEAN

A boolean type is either true or false Here are some examples:

$a = true;
$b = false;

ARRAY

Arrays are a list of other types of values (also known as lists, tuples and dictionaries). Here are some examples:

$a = [1,2,3];
$b = [1,'banana', 3.14];
$c = [[3,2,1]];

OBJECT

Objects are instances of a class which hold data about an object. Object types are user defined (as seen below) except for the stdClass (standard class) which is a generally available object type. Consider the following example:

class Cat
{
    public const AGE_MULTIPLIER = 7;

    public static $default_name = 'Buttons';

    public string $name;
    public int $age;

    public function getHumanAge(): int
    {
        return $this->age * self::AGE_MULTIPLIER;
    }
}

This class is named "Cat" and it's objects will contain and be instances of a cat.

// constants In the example above we see an example of a class constant named AGE_MULTIPLE with a value of 7. Class constants can be accessed without initializing an object (instance of a class). Constant values remains the same and cannot be changed.

// properties Properties are variables which belong to a class or object. To declare a property we need visibility, type, name, and optionally a modifier. Visibility has 3 possible values: public which is accessible from any scope, protected which is accessible within the current class and inheriting classes, and private which is only accessible within the current class. The type is any of the types we've described thus far and any class, in the example we have "string" and "int". The name can be anything you like (except reserved words), in the example these are "name" and "age". We may also optionally add the keyword static which tells us similarly to class constants that they can be accessed without initializing an object (instance of class).

Let's create an object from the class (blueprint) we have in the example above and see how to access the various values:

$my_cat = new Cat();
$my_cat->name = 'Zeus';
$my_cat->age = 7;
echo $my_cat->name . PHP_EOL;
echo $my_cat->age . PHP_EOL;
echo $my_cat->getHumanAge() . PHP_EOL;
echo Cat::AGE_MULTIPLIER . PHP_EOL;
echo Cat::$default_name . PHP_EOL;

On the first line we initialize a new cat object with the new keyword followed by the class name Cat and assign it to the variable $my_cat. The following 2 lines we assign the cats name Zeus and age 7. The next 5 lines are to output the various values (2 output properties, 1 output from method call, 1 output from class constant, 1 output from class static property).

The output from the above will be:

Zeus
7
49
7
Buttons

For more detailed information I would suggest visiting the official documentation: