How to declare variable in php?

Variables are used for storing values, like text strings, numbers or arrays. When a variable is declared, it can be used over and over again in your script. All variables in PHP start with a $ sign symbol.

Variable scope

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example:

 

Here the $a variable will be available within the included b.inc script. However, within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example:

 

Example  Using global

In PHP, a variable starts with the $ sign, followed by the name of the variable:

 

 

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *