Sunday, 27 January 2013

PHP Tutorial : Chapter 2 . PHP Syntax and variables (Part 2)

Suppose you have an expression like the following :-

$a = (9+5)*8/2+3-1*2+7 ;

S what will the expression evaluate to ? While evaluating , it should be kept in mind , that evaluation of expression occurs just like in normal mathematics , i.e. , first the expression within brackets are evaluated then division takes place , after that multiplication , followed by addition and ultimately subtraction . What happens in case the operators have same precedence ? Consider the following example :-

$a = 8.0/2.0/4.0 ;

In this case always remember that , the operations will be carried out from left to right . The expression will have a value of 1.0 and not 16.0 .
----------------------------------------------------------------------------------
To do Create an sample.php file in the following directory C:/wamp/www (the directory may differ based on where you installed wamp) , in case of linux go to /var/www and put the file in there . Now open the file with a text editor and key in the following :-


< html >
< head >
< title > Welcome to my first PHP Program < /title >
< /head >
< body >
< ?php
$a = (19+17)/(3*4)/(3) ;
echo $a ; // Echo takes a string as input and outputs it to the file
? >
< /body >
< /html >


First evaluate the expression yourself and check whether it tallies with the output . To run the file , open up a browser , and type localhost/sample.php .
----------------------------------------------------------------------------------

PHP offers us the following variable types :-

1) Integers . Eg:- 1,-1,0,2,6,-178 and so on
2) Doubles (Numbers with a decimal point) . Eg:- 1.78 , 3.9461 etc
3) Booleans , which can have only two values TRUE and FALSE .
4) NULL is a special type having only one value NULL (i.e., no value)
5) Strings , sequences of characters . E.g :- "This is a string"
6) Arrays , holds a collection of other values . E.g :- $a = array(1,2,"apple",TRUE);
7) Objects , instances of a class (Don't worry i'll explain this in much more detail later on)
8) Resources , variables holding stuff like database connection or stuff like that .

One should keep the following in mind about php variables :-

a) No explicit type declaration is required . For the ones who have programmed in other languages before you must have come across keywords like int,float while defining a variable . Well those are not required over here .

b) Automatic type conversion . PHP automatically converts type as and when required . For eg :-
$c = 3 + 0.14 ; will be evaluated to a double variable .

c) PHP assigns type depending on the situation . For example :-

$sub = substr (45981,2,3);
Substr is a function which extracts a string from another string where the string is the first input , and the next two inputs are the starting point and length of the string to be extracted . However in this case we gave a number so that should give us an error !! However php treats even that number as a string , and we have the output 981 . Thus php does type conversion based on the situation it is in , making it more flexible than other languages !!



No comments:

Post a Comment