PHP Bootcamp: Part 2 – Variables
Now that you understand the basics of PHP, we’re going to discuss variables more in depth, and look at the impact they have in coding for PHP. If you haven’t read part 1 yet, take a look at our first article here. We already know what variables are, so here are some points to consider when writing them out:
You do not have to declare variables.
This is good to know, since in other coding languages this is a necessity. All you need to do is assign a $ symbol in front of what your intended variable.
No two variables are the same.
PHP variables are case sensitive, something which can be a challenge with many lines of code.
Variables can happen anywhere.
Variables can be created anywhere; there is no limit to the number of variables that you can include in your line of code.
You do not have to define the variable type.
One of the perks of PHP is that the value type defines the correct data type. This saves time when you have many variables throughout your code.
Here you can see an example with two variables defining the expression “allergic” and “not allergic”. The operator in the middle (the equals sign) defines the terms of the expression.
Symbols Used in Defining Variables.
One of the first characters we see in the PHP language is the use of the “$” in front of the variable that you are trying to create. When creating a variable there are two rules that you need to follow: the variable needs to either start with a alpha character OR an underscore.
There are many different ways you can list variables in your code. It is not necessary to list the variables all at the beginning. You can also list variables in a layered format. In the example above, you can see a few unique variables laid out. These are considered Variable Layers. These can then be called to retrieve the desired variable. Two $s mean the second to the bottom, three $s mean the third from the bottom, and so on.
Using Echo with the previously defined variables gives similar instructions, but “echo” does just that; it echos the variables defined in the list.
Operators
When defining alpha variables, the use of the double quotations (“ “) is used after the equals sign. The equals sign is used as an operator to apply the value to the variable. (Fact: when using “==” in a comparison variable this is equivalent to equal or the values are the same, while the “===” means that the comparison operator defines the variable as identical as “!==” defines the variable as not identical and “!=” as not equal),
Other operators to consider when writing an expression:
With many lines of code and possible variables, any ambiguity can be resolved by using {} brackets.
Operators are frequently combined with other operators to reduce the total amount of code:
After this instruction, the value would equal 9. An important point to remember about operators is associativity (order precedence) in PHP coding. For example, increment and decrement operators (++ and –) are right associative, while all others like the ones listed above are left. When dealing with concatenates, interpolation (this uses whatever is within the double quotations and using that as the variable value; for example, “World” is the value of the variable of $Hello) is a better practice, and reduces the likelihood of future errors. PHP performs interpolation automatically when strings are enclosed by double quotations marks, but not single quotations.
There are two common types of variables that are used in PHP:
Global: Global variables can be used throughout the entire code
Local: Local variables are only used in a class
There are two exclusive PHP variables with unique rules that are particular to use:
Form Variables: $_POST,$_GET
Server Variables: $_SERVER
Something that is similar to other languages – such as Java and SQL – is the use of the semicolon. This is used to complete the line of code in order to define the action that the PHP coding is to complete or in this case, the variable to define. (Tip: You do not need to use the semicolon when defining number variables).
A completed expression of a variable does not include the semicolon. This is considered an incomplete expression, or just an expression. Only once a semicolon is added to the expression is it then considered a statement. There are several different types of expressions too, including ternary expressions, comparison, combined, and operator-assignment expressions.
Check out next week’s article when we will discuss data types!