PHP Variable is a container in which you can store any kind of information or data. PHP variables are also used to perform mathematical operations.
Table of Contents
How To Write PHP Variable
PHP Variable is written with dollar ($) sign, followed by the name of variable. PHP Variable name can be anything you want. However there are some rules that you must follow while naming PHP Variables.
Creating a PHP Variable
To Create a PHP Variable you would have to simply write the dollar sign ($) with the name of variable.
You can also assign this variable a value (obviously, that's what variables are for in PHP), for that equal sign (=) is used, followed by it's value. This is also known as Declaration of Variable.
<?php
$myVariable = "Hello World";
$x = 10;
$y = 20;
?>
As you can see, for text type data which is also known as String in programming term, we use double quotes.
Rules for Naming PHP Variables
Rules for naming PHP Variables are as follow.
- A variable always starts with the Dollar ($) sign, followed by it's name.
- A variable name can only have alpha-numeric characters and underscores (A-z, 0-9, and _ ).
- A variable name cannot have a number in start.
- A variable name must always start with a letter or the underscore character.
Note: Variable names are case-sensitive, means $name and $NAME are two different variables.
PHP Variables Scope
You can declare your PHP Variable anywhere you want but you have to take it's scope in consideration.
Scope of variable defines the fact that where you can use that variable in your code. There are three different types of PHP Variable Scopes
- local
- static
- global
local scope
PHP Variable declared within a function has a local scope and you can only access this php variable within the function.
static scope
A static PHP Variable declared within a function has a local scope and you can only access this php variable within the function but it doesn't loose the value assigned to it after the execution of the function unlike local variables.
statci keyword is used to define static php variables.
global scope
A PHP Variable assigned outside the function is only accessible outside the function and is known as global variable
To access global php variables inside function global keyword is used in declaration of php variable.
Mathematical Operations in PHP
In PHP, like all other programming languages, we can perform addition, subtraction, multiplication, division and other mathematical operations. In fact PHP is pretty powerful language when it comes to performing mathematical operations.
Addition in PHP
To add two php variables we use plus/addition symbol +.
<?php
$x = 10;
$y = 20;
$z = $x + $y;
?>
Subtraction in PHP
To subtract two php variables we use minus symbol -.
<?php
$x = 100;
$y = 20;
$z = $x - $y;
?>
Multiplication in PHP
To multiply two php variables we use multiplication symbol *.
<?php
$x = 100;
$y = 20;
$z = $x * $y;
?>
Division in PHP
To divide two php variables we use division symbol /.
<?php
$x = 100;
$y = 20;
$z = $x / $y;
?>
Note: PHP is loosely typed language. In PHP unlike other languages you don't have to tell the type of Variable you are dealing with, PHP takes care of that itself.
Concatenating PHP Variables
In PHP you can also join or concatenate two PHP Variables together. For this purpose concatenation operator (.) is used.
<?php
$x = "How To";
$y = "Code School";
$z = $x . $y; //How To Code School
?>
Case Sensitivity in PHP Variables
PHP Variable names are case sensitive which means that $name and $NAME are two different variables.
Always take care of Case-Sensitivity while naming and using your PHP Variables.
Outputting PHP Variables
In PHP we use echo or print statements are used to output the value of PHP Variable
The differences between echo and print is that echo has no return value while print returns 1 as value.
<?php
$x = 10;
$y = 10;
$z = $x + $y;
echo $z; // This will output 20
?>
We will learn more about echo and print statements in future articles.