PHP Variables

4 min read

A fundamental concept of any programming language is variables. Variables can contain values ​​of any kind, for example, simple lines or texts, but also more complex structures such as lists or even images. In this section, you will learn the basics of how to use variables, how to use them, and how to pass variables between two PHP pages.

The Basic

In PHP, texts or numbers are stored in so-called PHP variables or just variables, so that they can later be output using echo.

Variables in PHP always begin with a dollar sign ($), followed directly by the variable name, followed by an "equals sign" (=), and then the text enclosed in quotation marks ("), followed by the semicolon as the command terminator (;).

The variables can be given an arbitrarily appropriate name. However, attention must be paid to uppercase and lowercase letters. We would write the variables always small so that no confusion and thus no sources of error can arise.

For example, a variable in PHP might look like this:


<?php
$name = "Louis Armstrong";
?>

This example defines the $name variable with the content of Louis Armstrong.

It should be noted that variable names may start with a letter or an underscore. Numbers may not be used as initials.

If we "define" a variable twice, we override the content of the variable. Echo will only output the current content. Of course, the echo command must follow the variable definition, since PHP is an interpreted language and works the code line by line from top to bottom.

If the echo is in front of the variable, we try to output an undefined variable and get a blank output.

If specific settings are set, it can even happen that an attacker can inject arbitrary text.

Of course, you will need the command echo again for output. The script for this example looks like this:


<?php $name = "Louis Armstrong";
echo "my name is $name";
?>

As output we get:
My name is Louis Armstrong

Of course, we can also override the contents of a variable again by simply assigning the new value to the variable:


<?php
$name = "Louis Armstrong";
echo "First, my name is $name <br />";
$name = "Jacky Chan";
echo "Then my name is $name";
?>

The output would be:
First, my name is Louis Armstrong

Then my name is Jacky Chan

You can also attach an additional variable or text to an existing variable.


<?php
$name = "Louis ";
$name.= "Armstrong";
echo $name;
?>

The output: Louis Armstrong

If a variable or a text is to be "appended" to an already existing variable, this is done with a dot in front of the equal sign. This tells PHP that the text following the equals sign or the following variable should be appended to the existing variable. A variable can theoretically be extended infinitely often.

We can also use this directly in the echo:


<?php
$name = "Louis";
echo "My name is ". $name. " Armstrong";
?>

Here we have composite output. First, we output the first name, after the quotation marks we put a point and then the variable. After the variable, we can either end the output with a semicolon, but here we have added the text, Armstrong.

Of course, this is also possible with variables:


<?php
$color = "red";
$text = "We have one ". $color. " House";
echo $text;
?>

This method will be used more often later.

Variable types

You can define a wide variety of values ​​in variables. In this article texts (also called strings) were stored, in the next article numbers (also called integers) are stored in variables. PHP itself is a typeless programming language, i.e., PHP itself determines which type (text, number, etc.) has the value and sets it accordingly for the variable. You as a programmer do not have to worry about that.

Nevertheless, you should know which basic types of values/variables exist:

Integer: An integer variable contains only integers, i.e., numbers without a comma.
String: This is a variable that contains a text/sentence/ word.
Float: A floating-point number, i.e., a number with a comma (point = English).
Double: In PHP, it's the same as float.
Bool: These are the values true and false

For example:


<?php
$integer = 15; // an integer variable
$string = "a lot of text"; // a string
$float = 15.5; // a number with a point
$bool = true;
?>

« »