• Tutorials Logic, IN
  • +91 8092939553
  • info@tutorialslogic.com

Data Types and Variables

PHP Variables

Variables are "containers" for storing information. A variable can be used to hold values (eg. x=5) and expression (eg. z=x+y). PHP has no command for declaring a variable. We have to assign value to the PHP variable, while the creation of variable. If we want to create variable without assigning a value to it, then we assign NULL value to it. Generally in PHP, a variable does not need to be declared before adding a value to it.

Standard Rules for PHP Variable Names

  • Variable in PHP starts with a $ sign, followed by the name of the variable.
  • The variable name in PHP must begin with a alpha-numeric character or the underscore character i.e.(A-Z, a-z, 0-9, and underscore).
  • A variable name should not contain blank spaces.
  • A variable names are case sensitive i.e.($Y and $y are two different variable).

Scope of PHP Variables

Each variable has an area in which it exist, known as its scope PHP has four different variable scope:-

Local Scope:- A variable declared within a any PHP function is local and can only be accessed within that function.

										    
											<?php
											    $X=5 //Global
												
												function myFun() {
													echo $X //Local
												}
												
												myFun();
											?>
											
										

The above script will not give any output, because the echo statement refers to the local scope.