Lesson 6 – Variables in PHP
A fundamental concept of any programming language is variables. Variables in PHP can contain values of any kind, such as 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 in PHP, how to use them, how to expect them, and how to pass variables between two PHP pages.
How to use variable in PHP – Basics
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 terminator ( ; ).
The variables can be given an arbitrarily appropriate name, however, attention must be paid to uppercase and lowercase letters. I would always write the variables 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 = "Coding180"; ?>
This example defines the $ name variable with the content of Coding180.
It should be noted that variable names 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 need to echo command to the variable definition is because PHP is an interpreted language and executes the code line by line from top to bottom.
If echo is in front of the variable, we try to output an undefined variable and get a blank output.
If certain 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 = "Coding180.com"; echo "my name is $name"; ?>
As output we get:
My name is Coding180.com
Of course, we can also override the contents of a variable again by simply assigning the new value to the variable:
<? Php $name = "Robort Gabriel"; echo "First, my name is $name <br />"; $name = "Gabriel Robort"; echo "Then my name is $name"; ?>
The output would be:
First, my name is Robort Gabriel
Then my name is Gabriel Robort
So far, this feature would be interesting only for very long texts, for example, you want to address a person in 10 different places.
However, if we retrieve only one variable with echo (no more text) then we can (but do not have to) omit the quotation marks.
<?php $name = "Gabriel Robort"; echo $name; ?>
You can also attach an additional variable or text to an existing variable.
<?php $name = "Gabriel"; $name .= "Robort"; echo $name; ?>
Edition: Gabriel Robort
If a variable or a text is to be “appended” to an 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 = "Robort"; echo "My Name is ". $name." Gabriel"; ?>
Here we have a composite output. First, we give my name is out, 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 Robort.
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 in PHP
Variables can be defined in a variety of ways. 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 type-less programming language, ie 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.
However, you should know which basic types of values/variables exist:
Integer: An integer variable contains only integers, ie numbers without a comma.
String: This is a variable that contains a text / sentence / word.
Float: A floating-point number, ie a number with a comma. But you use the English spelling and thus a point instead of the German comma.
Double: In PHP the same as float.
bool: These are the values true and false
<? Php $integer = 15; // an integer variable $string = "a lot of text"; // a string $float = 15.5; // a number with a comma $bool = true; ?>
We are sorry that this lesson was not useful for you!
Let us improve this lesson!
Tell us how we can improve this lesson?