1:51 AM

Variables

posted under by FR3@K | Edit This
variables are data symbols carrying a value. in php variables are defined by using a $ sign before an identifier. identifiers are the names of variables. identifiers can't begin with a digit and may include letters, numbers, underscores and the $ sign. using the $ sign has a special meaning for variables (i won't mention about it now.) the name of a variable is case sensitive and you can't use function names as variable names. unlike the c language, in php you don't have to declare a variable before using it. the variable will be created when you first use it.

$myvariable=0;

this is a simple assignment to a variable named as 'myvariable'. php supports these data types:

integer
double
string
boolean
array
object

double is used for real numbers, string is a set of characters, boolean is a logical variable for values true and false, arrays are used for multiple number of variables of the same type and objects are used to store class instances. since php 4 the types NULL, boolean and resource are valid. variables that don't have any value, unset or given the value NULL are of NULL type. some functions return the type 'resource' that you use for handling some special data like database query outputs.
notice that we haven't used any type indicator before the name of the variable in the previous example. in php the type of a variable is the same as the value it has. for example:

$myvar=3;
$myvar=3.14;
$myvar="hello world";

the first statement (creates if not created yet and) makes $myvar an integer, the second statement makes it a double and the last makes it a string by assigning the values in the statements. you can also make type casting when you want the variable to be in an exact type.

$myint=1 ;
$mydouble=(double) $myint ;

the first statement gives the value to $myint as an integer and the second makes an assignment from $myint to $mydouble by casting the type to double although the value is the same actually. $myint is still an integer after type casting.

0 comments

Make A Comment
top