Monday, 6 June 2011

PHP Functions and Arrays


                                                              

                 FUNCTIONS
----------------------------------------------------------------------------------------------------------------
FUNCTION: - Reusable code module is called FUNCTION.
                                    (Or)
                       Self defined block of statements is called FUNCTION

use:-1)reusable any where in the program
       2) Save the time.
       3) Reduce the time, and complexity of the program.
Syntax:-
<?php
                        Function function name(parameters)
                        {
                          .........
                        ........
                        }
                        Function calling statement.
?>
type of functions:-
---->     2 types.
1)with out arguments.(return the value,with out return value)
2)with arguments.(return the value,with out return value)

how to know the php information:-
            <?php
        phpinfo();//easy
        (or)         
       phpinfo(INFO_MODULES);
?>



Structures:-
1. php does not directly support the structures
2. php class works like structures(c-type) as well as classes
<?php
class student
{
 $sno;
 $nsmr;
 $grade;
}
$s=new student;
$s->sno=10;
$s->name='hari';
$s->grade='A';
print_r($s);
?>\


                                                           ARRAYS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ARRAY(PHP):- An array is collection of variables indexed and bundled under one name.
example->1)   A (10,12,4,9,5)
                        0  1 2 3 4//locations
                 2)    b(10,'y',"apparao",4.6)
                          0  1         2            3   //locations.

in C,C++,JAVA,C# in general an array is collection of homogenious elements
which are stored contagious memory allocation under one name.


example:-        A 10,12,4,9,5//int
                       0   1 2 3 4//locations

2) How many type of arrays in php?
            3-types                        1)numeric array.
                                    2)associative array.     
                                    3)multidimensional array.
NUMERIC ARRAY:-
            DEF:-A numeric array stores each element with a numeric ID key.
            meaning:-a numeric array will take only values.
            EXAMPLE:-
            $myarray=array('apparao','raju',srinu','subbarao','mahesh');
basically php arrays are resources.
we should print the arrays by using print_r().

ASSOCIATIVE ARRAY:-
            in associtive array each id key is associated a value.
EXAMPLE:-
            $myarray=array("apparao"=>1,"raju"=5,"srinu"=2,"madhu"=>4,"hari"=3);
MULTIDIMENSIONAL ARRAY:-
            An array of arrays is called multi dimensional array.
                                    (or)
            An array with in another array is called multidimensional array.

CREATING AN EMPTY ARRAY:-
Syntax:-       $myarray=array();
range ():-
syntax:--          range(1,100);//it will return 1 to 100 values.

LIST():-       it is used to assign the array variables successively.
            $myarray=array('rose','marigold','lotus');
            list('red','yellow')=$myarray;

is_array():-     it takes one argument ,if it is array it will return 1 that means true
               other wise it doesnot return anything that means false.
count(),sizeof():-         both are identical,it will return the length of the array

unset():-          it will destroy the array (or)it will destroy the whole structure of the array

reset():-           it will return the pointer from start.  reset($myarray);//ex:-array8.php

current(),next(),prev(),end():-           ...................//ex:-array9.php

array_change_key_case($myarray):-          it will change the keys .if it is lower it will
print lower case ,if it is higher case but it will print lower case.//ex:-array10.php

*array_chunk():-       it will return the multidimentional array
<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
//print_r(array_chunk($input_array, 2, true));
?>

array_combine ($a,$b):-       it will take first array as keys and second arrays is values.
                                                ex;-array12.php

array_count_values($array):-          it will count the no of times the value is coming
                                                            ex:-array13.php

array_diff($array1, $array2):-        it will compare the two arrays,if the values are
different in both the arrays,it shows that values in the first array.

array_fill(5, 6, 'banana'):-    it will fill the array from 5 th position to 6 times banana
                                                //ex:-array15.php

array_flip($trans):-   it will interchange the key and values//ex:-array16.php

array_merge():-         it will merge the input array values//array17.php

array_reverse(array,true):-  it will return the reverse order of the array.

array_push():-           it will insert the elements into the stack

array_pop():-             it will remove the elements from the stack.
//see example :-array19.php

array_shift():-            this will shift the array elements.

imp:-   see array20.php example.

array_unshift():-        prepends passed elements to the front of the array
ex:-see array1.php exmaple

array_sum():-             add the elements.//ex:-array22.php

array_search():-         it will search what ever the element u want.see array23.php

srand():           it will generate the random numbers -see array24.php

array_slice:-array_slice($array,2):- it will print the element after the 2 elements

array_slice($array,0,3):-       it will return the elements from 0 to 3 position

Ksort ():-         ksort means keys sorting.it will arrange the keys asscending order.ex:-array26.php

rsort():-           it is applicable on the numeric array
                        it will arrange the array values in the descending order.//array27.php

sort():-             this is also applicable on the numeric array.it will return the values like
                        descending order//array28.php


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>...