Learn the variables , type of variables and scope of variable in PHP by this tutorial for beginner .A brief explanation of features and behaviour of PHP Variables .

PHP tutorial for variable scope and type of variable - Day 3 beginner

In the previous post that is Day2 ( Tutorial for php syntax overview) program, We learnt that what is php syntax overview and how we use on project.And Now it is Day3 Program, where we will learn variables and scope of variable in PHP language.

If you never had a programming knowledge, like Algebra, or scripting experience then the concept of variable is new concept to you.

Description for Variables :

  • Container of storing information.
  • There are several variable types, but most common is called String.
  • Start with $ sign.
  • A variable name must start with Letter or character and underscore but should not start with number.
  • It contains only alpha numeric characters and underscore (E.g.: A-z, 0-9, and _).

Syntax:

 

Note: Put quotes around the values, when you assign text as a value (“Hello_world”), and variables are case-sensitive.

Basic Example of Variable:

After Executing the above statement, the variable $Hello­_world will hold the value Hello world!, the variable $a will hold the value 2, and the variable $b will hold the value 8.

Learn about Output Variables:

  • To display the variable on the screen. Using echo Statement.
  • You will learn more about the echo statements in next chapter.

Example of Output Variables:

The following above example show output ( I love my website )when you execute this code.

Learn about Variable Scope.

  • In PHP we will find something named as scope.
  • When you declare variables, you declare them in a particular scope.  The scope is the area in which the variable is declared.
  • you set your scope with curly braces (the { and } characters)
  • You can create (declared) variable anywhere inside the script in the PHP.
  • It is a part of the script where the variable can be declare/referenced /used.

PHP has three different variable scopes:

  • Local variables
  • Global variables
  • Static variables

What is the Global and local Scope in php?

  • Global variables are declared outside a function. It can access any function in the program.
  • Local variables are declared only inside the function. It can access only that function, it can’t access other function in the program.

Example of Global Scope:

Example of Local Scope:

Note:You can use same variable name different function because they are only recognised by function in which they are declared.

What is Static Scope or variable?

PHP supports declaring function variables static. A static variable is shared between all calls to the function and is initialised during a script’s execution only the first time the function is called. To declare a function variable static, use the static keyword at the variable’s first use. Typically, the first use of a static variable is to assign an initial value:

Example of Static Variable:-

Output of above example for static variable:-

1
2
3

Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.

 

Other PHP Tutorials post :- 

<<<<< PHP tutorial for beginner Day 2 – Syntax overview and tag

<<<<< PHP tutorial for beginner Day 1 – Introduction and Installation.