Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Sunday, 9 June 2013

Array and Array Operations: PHP


Array creation:

To initialize an array in PHP:

$names_1  = array();   
                       
To add value to that array:

$names_1[] = "Bob";   
$names_1[] = "Marley";
$names_1[] = "Alex";
$names_1[] = "David";

You can also construct an array as follows:

            $arr1 = array(10,30,10,20,50);
           
To print the created array:

            print_r($names);


Array operations:
                       
To convert a string to array:

            You can use ‘explode’ to split an string with characters and add each element to array as below:

$names_2 = "Bob,Marley,Alex,David";
                       
$names_3 = explode("," , $names_2);   //--> String to array
print_r($names_3);                                                      
                       
To convert a array to string:

            If you want to get the elements from the array, and convert it into a string with each array elements separated by a character [say coma ‘,’], then you can make use of PHP ‘implode’.

print(implode(",",$names_1));               //--> Array to string
 
To eliminate similar data/elements from an array (array_unique):

$arr1 = array(10,30,10,20,50);

                       
$arr1 = array_unique($arr1);    //--> To eliminate similar data from the array
 print_r($arr1);
                       
To sort and reverse sort an array (sort and rsort):

            sort($arr1);                               //--> To sort an array
print_r($arr1);
                       
            rsort($arr1);                             //--> To reverse-sort an array
            print_r($arr1);

To add/ concatenate two arrays (array_merge):
                       
            $arr_1 = array(10,30,10,20,50);
            $arr_2 = array("Leon","CEO",41);
                       
            $arr_3 = array_merge($arr_1,$arr_2);  //--> To concatenate 2 arrays
            print_r($arr_3);

           
To get index key of an array as an array:

$arr_4 = array("name"=>" Leon ","destination"=>"CEO","age"=>41);
            print_r($arr_4);                        //--> Array with key index
            print_r(array_keys($arr_4));     //--> To get the index keys as an array


To check whether an element is in an array:

            $arr_5 = array("apple","orange","mango","grape","peach");
                       
            if(in_array("orange",$arr_5)){
     //--> To check whether an element is in an array
                        echo "Element Found...";
            }

Saturday, 8 June 2013

Basic functions : PHP


Some of the basic functions and data manipulation in PHP are given below:
 
$str_1 = " DeveloperDK Blogspot ";
  • echo "DK_".$str_1."_DK";                // --> DK _ DeveloperDK Blogspot _ DK
  • echo "DK_".trim($str_1)."_DK";       // --> DK_ DeveloperDK Blogspot _DK
  • echo strtolower($str_1);                    // --> developerdk blogspot
  • echo strtoupper($str_1);                    // --> DEVELOPERDK BLOGSPOT 
                       
$str_2 = "developerdk Blogspot";
  • echo ucwords($str_2);                       // --> Developerdk Blogspot
                       
$str_3 = "deVeloperdk BloGspot";
  • echo ucwords(strtolower($str_3));                 // --> Developerdk Blogspot
  • echo substr($str_3,0,5);                                 // --> deVel
  • echo substr($str_3,0,-4);                               // --> deVeloperdk BloGspot
  • echo substr($str_3,4);                                    // --> loperdk BloGspot
  • echo substr($str_3,-4);                                  // --> spot
                       
$str_4 = "Developer";
$str_5 = 11;
  • echo is_string($str_4);                        // --> o/p : 1
  • echo is_string($str_5);                        // --> no o/p                    

$str_6 ="100.00";
  • echo intval($str_6);                            // --> 100
                       
$str_7 = 2.3;
  • echo is_float($str_7);                         // --> o/p : 1

How to print data : PHP

You can use 'echo' to print string, variables, etc. in php. 

Syntax:

        echo "print it...";                       // Output------------->  print it...
        echo '<br/>';                           // prints a new line/enter

        $var_1 = 10;
        $var_2 = 3.14;
        $var_3 = "myString";
        echo $var_1;                           //  Output (number)------->  10
        echo $var_3;                           //  Output (string)------->  myString
        echo "$var_1 + $var_2";         //  Output (sum number)-------> 13.14
       
       echo '$var_1 + $var_2';          //  Output (string)-------> $var_1 + $var_2
       
       echo "$var_1 + $var_2 = ".($var_1 + $var_2);  
       // Output ---------> $var_1 + $var_2 = 13.14


      Note: dot(.) character is used to concant/join strings in php.