elibraryportal Logo

PHP Variable

PHP Variables are "containers" for storing information.

PHP Variables are used to store data, like string of text, numbers, etc.

PHP Variables

  • Some Rules for PHP variables:
  • PHP variable starts with the $ sign,
  • PHP variable name must start with a letter or the underscore character.
  • PHP variable name cannot start with a number.
  • PHP variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • PHP Variable names are case-sensitive ($age and $AGE are two different variables)

Example1

Output:-

PHP variables Example

Example2

Output:-

PHP variables Example

PHP Variables Scope

PHP has three different variable scopes:

  • Local
  • Global
  • Static

Local Variable Scope in PHP

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:

Example

Global Variable Scope in PHP

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:

Example

PHP The static Keyword

use the static keyword when you first declare the variable:

Example

Next Topic