Example -
PHP is a loosely typed language that means you do not need
to declare the data type of variables in php. PHP statements and PHP
variables are case-sensitive. All variables should start with $ sign. There are
4 types of variables in php.
1. local
2. global
3. static
4. parameter
Code and Syntax –
<?php
$x=5; // global scope
function myvarTest()
{
global $x; // access global variables from local scope
$x=5; // global scope
function myvarTest()
{
global $x; // access global variables from local scope
$p=10; // local variable
echo $x;
echo $x;
}
myvarTest();
myvarTest();
?>
Static variable example –
<?php
function myvarTest()
{
static $vx=0;
echo $vx;
$vx++;
}
myvarTest ();
myvarTest ();
myvarTest ();
?>
function myvarTest()
{
static $vx=0;
echo $vx;
$vx++;
}
myvarTest ();
myvarTest ();
myvarTest ();
?>
Parameter variable example –
<?php
function myvarTest($vx)
{
echo $vx;
}
myvarTest(45);
?>
function myvarTest($vx)
{
echo $vx;
}
myvarTest(45);
?>
Explanation
–
Above example shows how we can use local and
global variables in php.
This is how you can use variables in php.
For more
php questions and answers, dumps,tutorials for beginners and experienced people
visit http://www.top-php-interview-questions.blogspot.in If you find any errors while executing this
program, you can make your comments or mail me at reply2sagar@gmail.com
No comments:
Post a Comment