Tuesday, December 28, 2010

PHP

Tuesday                                                    PHP          
28/12/2010                              
 
Intro to php

Php is a server side scripting language most of the websites nowadays developing using php because it is free and open source it is major tough competition to asp.net and java it has lot of inbuilt features for creating dynamic web applications it will be very helpful now maximum share of the websites in the world using php scripting and  apache webserver because of its simplicity of code and extraordinary features of having it .acronym for php is “hypertext-preprocessor”

It is compatibility with all database servers mysql,sqlite,postgresql ,oracle and more

Syntax of php

              There are 4 ways of syntax declaring php are

   <?php  ?>

   <%    %>

  <script language=”php”>

   <?            ?>
Installation of php on windows

For developing web applications we need one server side scripting language and one  database server and the operating system whatever it may be and the webserver is apache or IIS whatever it may be

                   If you go through the xampp pack we get a bundled pack of  compenents:

           Apache+Mysql+PHP

So the developers need not worry about the installation of xampp pack in windows

Just download the xampp pack in apachefriends.org and select for windows and just save it in which drive you want

   C:/ and then change directory to xampp


After installing the xampp stack in windows u get a control panel of xampp stack just open and click the mysql and apache u enable and click start button to enable mysql and apache server
And then later

Open the firefox type this  http://localhost

You get the xampp stack web appearance in your firefox desktop

Open the notepad and type the small snippet of code save it your own filename and .php with extension

<?php
      
    Phpinfo();

?>

Save the php files in C:/xampp/htdocs/(“filename.php”)

And later check in the firefox of output of our programme just type the url location  http://localhost/(“yourfilename.php”)

Above programme shows the output of php version and php information in the server
Php in Ubuntu

Download the lamp stack from apachefriends.org and extract the tar file in

tar –xvzf  (latest version of lamp stack.gz file) –C /opt

u get a folder called lampp and as usual normal u go and type the code in normal note pad and save it in  filesystem/var/www with .php extension

if u not have permission to write any files to that folder

sudo chmod 777 –R /var/www




it will give write permission to that particular folder in /var file system

run the code in browser http://localhost/(filename.php)

Getting started with php

php has 4 data types of variables they are

1)    scalar

2)    compound type
              
3)special data type

Scalar data types are
                                   Integer,float,string,double
Compound date types
                                    Array,Object
Special data types
                                    Resource,Null

First exercise in php

<?php

   echo $_SERVER[‘HTTP_USER_AGENT’];

?>
      Normally php variables started with $symbol the above code output wil displays the browser information of which browser we using currently

Arrays in php

              Php have 3 types of arrays they are
                  1)Numeric array

                 2)Associative array

                 3)Multidimensional array


1)Numeric array

           It is ordinary array just we declaring the array values as usual in other languages example
 
         <?php

         $myarr = array(‘chennai’,’mumbai’,’calcutta’,’delhi’);

        echo  $myarr[0]
         
        echo  $myarr[1];

        echo  $myarr[2];

         ?>

The above code represents we declaring the variable $myarr and assigning the values of city names chennai,mumbai,calcutta and delhi

And printing the values of  the variable $myarr[] and the position of the array

The output for the above programe is Chennai,Mumbai,calcutta

2)Associative array

                    In this array we assign the key values to them

Example
  
           <?php

         $myarr=array(‘sachin’=>45,’sehwag’=>75,’talent’=>’ap’);

                     echo  $myarr[‘sachin’] ;
                      
                     echo  $myarr[‘sehwag’];

                     echo  $myarr[‘talent’];

                ?>        

We assign the key values to the above variable in $myarr we will get the output  45,75 and ap

3)Multidimensional Array

  We declare array within array is called multidimensional array php has nice feature of this

 Example
 
 <?php

  $myarr=array(“john”=>array(‘family’=>’chennai’,’age’=>32,’code’=>45),

  ‘smith’=>array(‘name’=>’school’,’age’=>42,’code’=>23));

                       echo  $myarr[‘john’][‘family’];

                       echo  $myarr[‘john’][‘age’];
                          echo  $myarr[‘john’][‘code’]; 

                         echo  $myarr[‘smith’][‘code’]; 

                          echo  $myarr[‘smith’][‘name’]; 

?>
The above programme output is Chennai,32 ,45,23,school