Core PHP PHP

PHP Variables

A variable is a name which contains values or information

PHP variables:

  • PHP variable starts with “$” sign.
  • PHP variable name must start with a alpha letter.
  • PHP variable name cannot start with a numeric value and underscore “_”.
  • PHP variable name can only contain alpha, numeric and underscores value (A-z, 0-9, and _ ).
  • PHP variable name are case sensitive ($name and $NAME are two different variables).

<?php

$name = “Testing for PHP variable”;

echo $name;

?>

There are three types of variable:

  1. local
  2. global

Local Variable

Local variable declare or accessed within a function only. If you declare a variable in function and access outside of function it will notice error “Notice: Undefined variable”

Example :

<?php

function abc(){

$name = “Testing for local variable”;

}

echo $name;

?>

Global Variable

Global variable is used with the help of GLOBAL key word.

<?php

$a = “PHP global variable”;

$b = ” with the keyword GLOBAL”;

$c = “”;

function globalVariable(){

global $a;

global $b;

global $c;

$c = $a.$b;

}

globalVariable();

echo $c;

?>

Leave a Reply

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