Saturday, 4 June 2011

PHP Variables and Operators


                                    

syntax(or)structure:-
<?php
..........;
..........;
?>

RULES:-
1------>file should be save with .php extension
2------>tags should be compulsory (<?php     ?> OR <?  ?>)
3------>every statement should be end with ;
Printing statements in php:-
print "------";//perl type
printf("-----");//c type
echo "-----";//php(may be unix or perl)

Variable declaration:-
$a=10;     //2 bytes memory
$b="hello";//5 bytes memory
$c=10.6;   //4 bytes memory

Comment lines in php:-
 php follows c style comments
 for single line: //
 for multiple lines: /*     */
php is case insensitive language & some times case sensitive language.
php ignore the white spaces.(php is white space insensitive)

PHP OPERATORS:-
php supports what ever the operator supported in c.
1->Arithmetic operators (+,-,*,/,%,++,--)
2->Assignment operators (=,+=,-=,*=,/=,%=)
3->comparison operators (==,!=,<=,>=)
4->logical operators (&&, | |, !) ex :- !(x==y)-->returns true.
5->identical equal to (===), not identical equal to (!==)

PHP CONTROL FLOW STATEMENTS:-
1-->conditional structures
2-->Iterative structures

conditional structures:-
1-->if (condition)
                        {     ...
                              ...
                        }

2-->if (condition){      ....
                                                ....
                                        }
                       else          {
                                               ...
                                                ...
                                       }
3-->if(condition){
                                                ....
                                                ....
                                       }
                        else if (condition)        {
                                                                        ...
                                                                        ...
                                                            }
                        else if(condition)         {
                                                                        ...
                                                                        ...
                                                            }
else      {
                                       …………....
                                      …………....
                                    }

             4-->ternary operator
                        (Condition)? statement1:statement2;
                                        |->(true)    |->  (false)
5-->switch case:-
                        switch(option)
                        {         
Case 1:....
                                           Break;
                                    Case 2:.....
                                          Break;
                                    Case 3:.....
                                          Break;
                                    default:.....
                        }

Interview) what is different between echo and print?
Ans)    1) echo can't be used in the middle of the expression.
                        While print can be used.
            2) Echo can take multiple expressions, but print can’t take multiple expressions
            3) Echo performance is high than print .bcoz it does not return any value.
         Print returns the Boolean value
Iterative structures:-
1-->while(condition)
                        {            ........
                                      ........
                        }

2-->do
                        {
                          ........
                          ........
                        }
                        while(condition);

3-->for(initialization;condition;increment/decrement)
                        {   ........
                             ........
                        }

4-->foreach($a  as  $k=>$v)
                        {
                           .......
                           .......
                          }
         
note:-             $a--->array.
            $k--->current index of $a.
            $v--->value at index $k.
         
Note:-generally for each will use for arrays
Interview) what is the difference between while ,do while?
Ans )         while loop first check the condition after the statements will execute
                  but do while loop, first do part will execute after check the condition.

No comments:

Post a Comment

Visual comparison of the two methods, creating a simple table.

Option 1, using PHP: // PHP $html = '<table>' ;     foreach ( $data as $row ) {     $html .= '<tr>...