PHP Variable scope

PHP Variable &  Declare Variable:

In this php course we learn about php variable using algebraic terms. In algebra we use “p” “q” like these alphabets to hold different numeric values like “2” “3” . Lets Suppose we assume a formula  r=p+q so we will get 5 out of this formula. Lets take this formula in php variable example.

php 
$p=2; 
$q=3; 
$r=$p+$q; 
echo $r; 
?>

Above php code “$” declare the variable. php case-sensitive that’s why $P & $p is two different php variable.Keep remember in mind uppercase and lowercase alphabets not declare same variable. php variable name cannot begin with numbers.

Declare Variable:

php 
$name="Knowledge idea";
$data="Jhon have two tablets";
echo $name;
echo $data;
?>

Note: when you appoint a text to variable so put quotes around the text. check the below highlighted example.

php 
$text="Put quotes around the text";
?>

PHP is a closely language because above all examples we didn’t declare that which data type and name of the variables before to utilize it.On the other hand-side in Java,C++ a developer must declare data type.

Local & Global area variable declaration:

A variable declare outside of a function has a global area and can accessed only outside of this function. A variable declare Inside of a function has a Local area and can accessed only Inside of this function. Lets take an example both areas.

php 
$p=2; //Global Area & it access only outside of area
function take demo() {
$q=3; //Local Area & it access only inside of area
echo "demo variable inside the function";
echo "Variable of $q";
echo 
;
echo "Variable of $p";
}
take demo()
echo "demo variable outside the function";
echo "Variable of $p";
echo 
;
echo "Variable of $q";
?>

Above line”6″ highlighted shows Local Area/scope.Second below line”12″ shows global Area/scope. Grab these above codes and try it yourself !!

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments