# How To Declare PHP Variables

In this tutorial, I will be explaining how to write variables in PHP.

Variables are symbols that stand for something. You can think of them basically as containers for storing information.

You are free to assign whatever values you wish, since PHP does not have fixed types.

***Declaring Variables In PHP***

The syntax for writing a variable in PHP is $x, where $x is the name of the variable.

The notable thing is that you must have a dollar sign in front of your word. You are free to call it whatever you want(other than some reserved keywords) so long as it has a dollar sign in front of it.

You can also use any combination of capital letters and lower case letters. Usually, if a variable will contain more than one word, it is a good idea to separate them via an underscore.

Remember that variables are case-sensitive. This means that `$ABC`, `$ABc`, and `$abc` will be completely different variables.

And as a random aside, $ cannot be a variable name.
***Example***
```
/*below are valid variable names except for the third*/
$abc
$ABC(!) 
$1abc$_abc
```

***Types of Variables in PHP***

So what is a variable type?
Essentially, a variable type is the type of data that the variable stores.

If you are familiar with any other programming language, you can more or less guess what types of data type there are.

PHP isn’t too different from any other language in that it supports all the basic standard data types.

***PHP Is A Weakly-Typed Language***

If you have worked with a strongly typed language such as Java, you might remember that you must declare the specific data type for every variable you create.

PHP, by contrast, is a weakly typed language. This means that you do not have to specify variable types when you create them.

In fact, PHP allows you to mix floats and integers together.

***Example Code***
```
<?php
$height = 2.5; 
$width = 5;
$area = $height*$width;
echo $area;
?>
```
***Result***
![1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1634076646449/tikOdgx_b.png)

We didn’t declare the data type of `$area`, however, the area will be a float due to `$height`.
In strongly typed languages, the above would have given you an error.
In fact, you are free to reassign a different type of data at a later time.
***Example***
```
<?php$a = 0;
$a = 0.00;?>
```
All the code above is legal.
However, had you been using a strongly typed programming language, the above would have given you an error. But since PHP is weakly typed, so you are free to assign it any value you want.
In fact, even the code below is perfectly legal.
***Example***
```
<?php
$a = 0;
$a = "word";
?>
```
You can even assign a string value to a variable that currently stores an integer.

***Assignment By Reference***

A cool trick that you can use in PHP is to connect the values of two variables together.
Let’s say we have two variables `$a` and `$b`. If I assign the value of `$b` by reference to `$a`. This means “I give the value of `$b` to `$a`, and whenever the value of `$b` changes to something new, the value of `$a` will also change to that new value.”

***Example***
```
<?php
$b = "abc";
$a = &$b;
$b = "def";
echo $a;
?>
```
***Result***

![2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1634076642740/4CFsMeDEm.png)
The code above prints out “def”.

***Variable Scope***

Where you declare a variable will determine where you can use it.
If you declare a variable inside a function, this is called local scope.
A variable declared inside a function can only be used within that function, it has no meaning outside the function.
A variable not declared in any particular function can be used anywhere except inside a function.
To use a variable not inside a function, you must use the global keyword.
***Example***
```
<?php
$a = "a";
$b = "b";
function x()
{
$a = "inside";
echo $a;
}
x();
echo "<br>";
echo $a;?>
```

***Result***

![3.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1634076680737/Vd0GiyDEA.png)
The code above will actually display “insidea” because the `$a` inside of the function `x()` has nothing to do with the `$aoutside` of `x()`.
Essentially, variables inside a function and variables outside that function have nothing to do with each other.
However, as mentioned briefly above, there is actually a way that you can use a global scope function inside a local function; and once you are done with the variable inside the function, it will retain the same value outside the function.
The global Keyword
When you declare a variable, you can use it everywhere except inside a function.
If you want to use a variable inside a function, you must “redeclare” it inside the function using global.
Then, inside the function, that variable will have the same value it had outside.
And once outside the function again, the variable will retain the same value it had inside.

***How To Use Type Casting***

One neat little trick you can use is to have a variable pretend to a different type.
```
<?php
$a = 135;
$b = (float)$a;var_dump($a);echo "<br>";var_dump($b);?>

```
***Result***

![4.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1634076690961/4njUilfz2.png)

`$b` is now storing 135 as a float while `$a` remains an integer.
In summary, the scope of a variable is: where can this variable be used?

***Summary Of Variables Rules***

Here are some rules on to keep in mind about where variables can be used:
Built in variables can be used everywhere.
Constants can be used everywhere once you declare.
A variable you declare outside any function can be used, by default) anywhere except within a function. Within a function, none of the outside variables are recognized unless you declare them in using global or get them using $_GLOBAL[].
A variable you declare inside a function can only be used in that function (this is a common source of errors).

***Built In Variables***

PHP not surprisingly includes some built in variables
In my opinion, do not memorize them. Rather use them when needed and learn them in context.

```
$_GLOBALS[]
$_SERVER[]
$_GET[]
$_POST[]
$_COOKIE[]
$_FILES[]
$_ENV[]
$_REQUEST[]
$_SESSION[]
```

***Static Variables***
Normally, when a function ends, a variable inside loses its value.
However, sometimes we might wish to store them for further use.
To do this, you must use the static keyword.
This means after the function has finished executing, the variable will retain the same value if the function is ever executed again.
This is often used with a counting function where you use something such as `$x++`.

***Example***
```
<?php
function a_count()
{
static $a = 1;
$a++;return $a;}echo a_count();echo "<br>";echo a_count();
?>
```
***Result***
![5.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1634076858800/WMIIGnT1U.png)

