The ability to stores values in variables is a crucial aspect of most programming languages. It allows you to store numbers, strings, objects and more for later use in processing.
Variable declaration
In PHP a variable declaration starts with the character "$" followed by the name you wish to give your variable. A variable can be named anything as long as it meets the following criteria:
- A variable name must only contain letters, numbers or underscores. Any other characters cannot be added.
- Variable names must start with either a letter or an underscore, not a number
PHP variables are also case sensitive which means the variable $car is different from the variable $CAR.
PHP valid variable names
$dog
$DOG
$dog_name
$dog123
$_dog
PHP invalid variable names
dog
$dog*
$dog&
Assigning values to variables
PHP does not require you to specify a data type (eg. integer, string) like other programming languages such as Java. Variables can store data types such as strings, integers, booleans, floats, objects and arrays.
$text = "hello world"; // this stores a string
$number = 1; // this stores an integer
$bool = true; // this stores a boolean
$decimal = 12.53; // this stores a float
$arr = array(1,2,3); //this stores an array of values of any type
class Person{
function Person(){
this->name="john";
}
}
$person1 = new Person(); // this stores an object
Combining variables
Sometimes it is necessary to use variables to perform arithmetic procedures, to combine strings or other functions, to do this we must be able to combine variables with other variables and values.
Combining string values
To add one string into another string value, we can use the "." operator.
$string1 = "hello";
$string2 = "world";
$outputString = $string1." ".$string2;
echo($outputString);
In the above example we have two strings, one containing "hello" and the other containing "world". We then create another string which we assign a value of a combination of the two initial string variables with a space in-between. This will output "hello world".
Combining string values (short hand)
When you only need to add one string to the end of another without the need for specific positioning, we can make use of the ".=" operator which will concatenate one value onto the end of another.
$outputString = "hello";
$outputString .= " world";
echo($outputString);
// this is the same as:
$outputString = "hello";
$outputString = $outputString." world";
echo($outputString);
The above example will again output "hello world".
Performing arithmetic functions with numbers
With PHP we can add, subtract, multiply, divide, modulus and more with integers and floats. We can also use these operators between floats and integers, in most cases the answer will just be converted to a float.
$num1 = 6;
$num2 = 10;
$float1 = 2.5;
echo( $num1 + $float1 ) //for addition, outputs 8.5
echo( $num1 - $float1 ) //for subtraction, outputs 3.5
echo( $num1 * $float1 ) //for multiplication, outputs 15
echo( $num1 / $float1 ) //for division, outputs 2.4
echo( $num2 % $num1 ) //for remainder after division, outputs 4
Performing arithmetic functions (short hand)
If we need to add, subtract etc. one variable with another variable we can use an operator similar to ".=" for each operation
$num = 5;
$num += 2; // $num is now 7, identical to: $num = $num + 2
$num -= 1; // $num is now 6, identical to: $num = $num - 1
$num *= 2; // $num is now 12, identical to: $num = $num * 2
$num /= 2; // $num is now 6, identical to: $num = $num / 2
$num %= 5; // $num is now 1, identical to: $num = $num % 5