PHP Variables

tutorial and example
tutorial and example

PHP Variable is the name of memory location used to hold the data.  In PHP, variable starts with $ symbol.

Rules for PHP Variables.

  • Variable starts with $ sign.
  • Variable name must start with a letter or the underscore character.
  • Variable name can contain alpha-numeric character (A-Z. 0-9).
  • Variable name are case-sensitive ($name and $NAME are two different variables).

Example

Output

PHP Father: RasmusLerdorf

In PHP, there are three variables:

  • local variable
  • global variable
  • static variable

Local variable

PHP local variable is declared inside a function. It has local scope and can be accessed within the function.

Example:

Output

x variable inside of the function: 10
Notice: Undefined variable: x in C:\xampp\htdocs\crud\HELLO.PHP on line 8
x variable outside of the function:

 Global Variable

The Global is a keyword that is used to access global variable within a function.

Example:

Output

55

Static variable

Php static is a keyword that is used to create a static variable. Static variable is not be deleted when a function is completed /executed.

Example

Output

0

1

2