Php Variables
PHP Variables : Php training in Chandigarh
Today we will discuss about Php variables with php training in Chandigarh.
There is a Holder for a Variable Data Type. Therefore, depending on its type, one can hold the resources
of the Variable Numbers, Strings, Booleans, Objects or it can NULL. All the variables in PHP start with
a sign "$" and in this, value can be assigned using "=" operator. Dollar Sign is not technically a part of
the Variable's name but it is necessary to recognize such variables as the first character of PHP Parser.
of the Variable Numbers, Strings, Booleans, Objects or it can NULL. All the variables in PHP start with
a sign "$" and in this, value can be assigned using "=" operator. Dollar Sign is not technically a part of
the Variable's name but it is necessary to recognize such variables as the first character of PHP Parser.
One more important thing in PHP is that all the statements have a semicolon ";" Should end with the same.
In PHP, we do not have to specify the type of variable as it takes the Data type from Assigned Value.
In PHP, we do not have to specify the type of variable as it takes the Data type from Assigned Value.
To declare a variable, you must include it in your script. You can Declare a Variable and assign it to the same
Statement with a value.
Statement with a value.
Rules for Variable Declaration
- Variable starts with $ sign and with Variable Name
- Variable starts with the Letter or Underscore sign followed by Underscore Number or Characters.
- Variable Numbers do not start with Variables can be defined using alpha-numeric characters and
- only by Underscores.
PHP Variable Scope
There is nothing except Scope only Rules when the names of two different places have the same meaning.
There are three Variable Scope to define Places in PHP.
There are three Variable Scope to define Places in PHP.
- Local Variable Scope
- Global Variable Scope
- Static Variable Scope
Local Variable Scope
Local Variable is defined and accessed within the Function. This is not accessible beyond the Function.
For example
<! DOCTYPE html>
<html>
<head>
<title> Local Variables Example </ title>
</ head>
<body>
<? php
function myTest () {
$ x = 6;
echo "<p> variable x inside function is: $ x </ p>";
}
myTest ();
echo "<p> variable x outside function is: $ x </ p>";
?>
</ body>
</ html>
Global Variable Scope
The Global Variable is defined beyond the Function. You can use Global Variable anywhere in the script
outside the Function. But the Global Variable can also be accessed inside the Function before using the
Keyword Global before Declaring the Variable.
outside the Function. But the Global Variable can also be accessed inside the Function before using the
Keyword Global before Declaring the Variable.
For example
<! DOCTYPE html>
<html>
<head>
<title> Global Variables Example </ title>
</ head>
<body>
<? php
$ x = 6;
function myTest () {
echo "<p> variable x inside function is: $ x </ p>";
}
myTest ();
echo "<p> variable x outside function is: $ x </ p>";
?>
</ body>
</ html>
Static Variable Scope
Generally, when a Function is completed / executed, all the variables are deleted. However,
sometimes we do not want to delete a Local Variable. We want it for furthermore. To do this, you
have to declare the variable by using Static Keyword and before
Read More @ Php training in Chandigarh
sometimes we do not want to delete a Local Variable. We want it for furthermore. To do this, you
have to declare the variable by using Static Keyword and before
Read More @ Php training in Chandigarh
This comment has been removed by a blog administrator.
ReplyDelete