PHP Variables
Variables are important to PHP. A variable is a way of storing a value, such as text (“Hello”), numeric values (such as the integer value of 1) and also Boolean values (true and false). A variable can be re-used throughout your code instead of typing out the value over and over again. Variables will also not be new to you if you’ve had any experience of Algebra in Mathematics.
You define a variable in PHP like so:
<?php $name = “Marc Fraser”; $age = 16; ?>
The first things you should notice is the dollar sign ($). In PHP this signifies that the item is a variable, I can name a variable whatever I wish such as $a, $b, $c, as long as I do not omit it – otherwise syntax errors will occur. At this time, you should also note that variables are case sensitive, therefore $a is different in the eyes of PHP than $A.
Once our data is stored in a variable, we can perform operations to it, such as print, string manipulation and if the variable is an integer, calculations can be done. Below is how to print a variable:
<?php $name = “Marc Fraser”; $age = 16; print $name; print $age; ?>
The above will output “Marc Fraser16”.
Variable naming conventions:
- PHP Variables may only start with a letter or underscore (_).
- Spaces are not permitted in PHP variables and so underscores, or capitalization should be used – such as: $this_variable, $thisVariable.
- Variables may only comprise of alphanumeric characters.
Introduction to PHP, the basics. [Full Article]
Introductions
Article Scope
This article aims to express the basics, which a beginner should consider when learning PHP. It will explain how to create a very basic PHP page that will contain fundamental commands to PHP beginners.
For this article, I will be using XAMPP to execute my PHP code – this is free to download and is great, especially if you do not have a web server with PHP installed.