Variables in PHP
1) As with other programming language, PHP has the provision to define variable and manipulate
we have several types of variables in PHP,the most common used one is the string. It is use to store both the text and number
all the variable start with $ sign
Example
$tot= “This is total”;
we have several types of variables in PHP,the most common used one is the string. It is use to store both the text and number
all the variable start with $ sign
Example
$tot= “This is total”;
$t1=123;
Everything inside the double quotes is assigned to the variable string tot
2) Variable names are case sensitive.so $tot and $TOT are not same
3) Variable name can contain any letters,character ,underscore but it should not start number. It cannot contain special character like #,%
Example $1=”this is not valid “; is not allowed
4) Other variable data type are integers,boolean,null,doubles,strings ,array,objects and resources
5) Variables can, but do not need, to be declared before assignment
6) Variables in PHP do not have intrinsic types – a variable does not know in advance whether it will be used to store a number or a string of characters.
$test=1;
Most other languages require that you initialize the variable before using it, specifying what type of data it can hold, but PHP is more informal. You don’t need to tell PHP which data type is in a variable. PHP evaluates the data when you assign it to the variable and then stores it as the appropriate type. Generally, this is helpful. PHP guesses the data type pretty accurately.
$test=1;
Most other languages require that you initialize the variable before using it, specifying what type of data it can hold, but PHP is more informal. You don’t need to tell PHP which data type is in a variable. PHP evaluates the data when you assign it to the variable and then stores it as the appropriate type. Generally, this is helpful. PHP guesses the data type pretty accurately.
7) PHP does a good job of automatically converting types from one to another when necessary.
Example
<?php
$x=1;
$y=1.1;
$z= $x +$y;
echo “$z”;
?>
$x=1;
$y=1.1;
$z= $x +$y;
echo “$z”;
?>
Here x is integer type while y is float type, while adding PHP automatically converts the integer type x to float and add the variable
8) The scope of the variable could local,global and static
Local variable are variable defined in function and there scope is limited to the function. They cannot be used outside the function
Two function can use same name of the variable as there scope will be limited to function only
Two function can use same name of the variable as there scope will be limited to function only
<?php
function test()
{
$x=2;
echo “$x”;
}
{
$x=2;
echo “$x”;
}
test();
//the below statement would generate error as variable x is local to function
echo “$x”;
?>
//the below statement would generate error as variable x is local to function
echo “$x”;
?>
Global variable are variable which are defined outside function. They can be used only inside the function using global keyword
<?php
$y=2;
function test()
{
$x=2;
// this will generate error global variable is not available to function
echo “$x to $y”;
}
$y=2;
function test()
{
$x=2;
// this will generate error global variable is not available to function
echo “$x to $y”;
}
test();
?>
<?php
$y=2;
function test()
{
$x=2;
global $y
// this will not generate error as global keyword is being used now
echo “$x to $y”;
}
$y=2;
function test()
{
$x=2;
global $y
// this will not generate error as global keyword is being used now
echo “$x to $y”;
}
test();
?>
Static variable : When the function is executed ,the local variable values are destroyed upon completion of function,but if you dont want the local variable to be deleted then use the static keyword with the local variable in the function
0 comments:
Post a Comment