Scope of Variable in PHP:

The one important thing is that PHP is a loosely type language means you don’t have to define the data type of a variable it will automatically converts the variable with correct data type.

Now “what is the scope of a variable in PHP?” so the answer is that you can declare a variable anywhere in the script. PHP has three type of variable declaration which is as follows:

  • Global
  • Local
  • Static

Global & Local Scope of a Variable:

A variable which is declared outside a function has a global scope and can be used or accessed outside a function and a variable which is declared inside a function has a local scope and can be used only inside the function.

Example:

The Static keyword in PHP:

In PHP when a function is executed all variables of that function are deleted. But sometime we want that variables for further use mean we don’t want to delete the variables after execution of function. At this time we declare that variable with keyword “static”.

Example:

The global keyword in PHP:

You have learn that you can not access global variable within a function but if you want to use them in a function you have to call them with “global” keyword within that function.

Example:

 

3 responses to “Scope of Variable in PHP”

  1. Louis Tnago says:

    Wow, suprisingly I never knew this. Keep up with good posts.

  2. Sam H says:

    I am impressed with this information and would gladly read more from you. You are a good writer with a lot of talent. Thank you.

  3. Margeret Thie says:

    Only wanna remark that you have a very nice web site, I love the style it actually stands out.

Leave a Reply

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