03 Php Array String Functions

4,124 views
3,980 views

Published on

Basic concepts of ARRAY and String functions in PHP

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,124
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

03 Php Array String Functions

  1. 1. PHP Day 3 Geshan Manandhar Developer, Young Innovations Pvt. Limited www.geshanmanandhar.com http://www.php.net
  2. 2. PHP Strings <ul><li>A string is series of characters. </li></ul><ul><li>In PHP, a character is the same as a byte, which is exactly 256 different characters possible. </li></ul><ul><li><?php </li></ul><ul><li>$s=“I am a string”; </li></ul><ul><li>$s2=‘I am also a string”; </li></ul><ul><li>print $s.”---”.$s2; </li></ul><ul><li>?> </li></ul>
  3. 3. PHP Strings <ul><li>Another Example </li></ul><ul><li><?php </li></ul><ul><li>$beer = 'Heineken'; </li></ul><ul><li>echo &quot;<br>$beer's taste is great.&quot;; // works, &quot;'&quot; is an invalid character for varnames </li></ul><ul><li>echo &quot;<br>He drank some $beers.&quot;; // won't work, 's' is a valid character for varnames </li></ul><ul><li>echo &quot;<br>He drank some ${beer}s.&quot;; // works </li></ul><ul><li>echo &quot;<br>He drank some {$beer}s.&quot;; // works </li></ul><ul><li>?> </li></ul>
  4. 4. Important String Functions <ul><li>explode (string $delimiter, string $string) </li></ul><ul><li>nl2br ( string $string ) </li></ul><ul><li>strcmp ( string $str1, string $str2 ) </li></ul><ul><li>strlen ( string $string ) </li></ul><ul><li>strtolower ( string $str ) </li></ul><ul><li>substr ( string $string, int $start [, int $length] ) </li></ul><ul><li>trim ( string $str ) </li></ul><ul><li>Example code at day03prog22_string_functions.php </li></ul>
  5. 5. Array <ul><li>In computer science an array is a data structure consisting of a group of elements that are accessed by indexing. </li></ul><ul><li>In most programming languages each element has the same data type and the array occupies a contiguous area of storage. </li></ul><ul><li>Most programming languages have a built-in array data type. </li></ul>
  6. 6. Array <ul><li>?php </li></ul><ul><li>$fruits_1 = array (&quot;Apple&quot;, &quot;Mango&quot;, &quot;Banana&quot;); </li></ul><ul><li>$fruits_2 = array ( </li></ul><ul><li>0 => &quot;Appple&quot;, </li></ul><ul><li>1 => &quot;Mango&quot;, </li></ul><ul><li>2 => &quot;Banana&quot; </li></ul><ul><li>); </li></ul><ul><li>$fruits_3[] = &quot;Apple&quot;; </li></ul><ul><li>$fruits_3[] = &quot;Mango&quot;; </li></ul><ul><li>$fruits_3[] = &quot;Banana&quot;; </li></ul><ul><li>?> //Program code: day03prog23_array_more.php </li></ul>
  7. 7. Multi-Dimensional Array <ul><li><?php </li></ul><ul><li>$shop = array( array( Title => &quot;rose&quot;, </li></ul><ul><li> Price => 1.25, </li></ul><ul><li> Number => 15 </li></ul><ul><li> ), </li></ul><ul><li> array( Title => &quot;daisy&quot;, </li></ul><ul><li> Price => 0.75, </li></ul><ul><li> Number => 25, </li></ul><ul><li> ), </li></ul><ul><li> array( Title => &quot;orchid&quot;, </li></ul><ul><li> Price => 1.15, </li></ul><ul><li> Number => 7 </li></ul><ul><li> ) </li></ul><ul><li> ); </li></ul><ul><li>?> //full code at day03prog24_array_multi.php </li></ul>
  8. 8. Important Array Function: asort <ul><li>asort($array) – Sort an array and maintain index association </li></ul><ul><li><?php </li></ul><ul><li>$fruits = array (&quot;d&quot; => &quot;lemon&quot;, &quot;a&quot; => &quot;orange&quot;, &quot;b&quot; => &quot;banana&quot;, &quot;c&quot; => &quot;apple&quot;); </li></ul><ul><li>asort($fruits); </li></ul><ul><li>foreach ($fruits as $key => $val) { </li></ul><ul><li>echo &quot;$key = $val &quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  9. 9. Important Array Function: push/pop <ul><li>array_push($array, element1,…) </li></ul><ul><li><?php </li></ul><ul><li>$stack = array(&quot;orange&quot;, &quot;banana&quot;); </li></ul><ul><li>array_push($stack, &quot;apple&quot;, &quot;raspberry&quot;); </li></ul><ul><li>print_r($stack); </li></ul><ul><li>?> </li></ul><ul><li>array_pop($array) </li></ul><ul><li><?php $stack = array(&quot;orange&quot;, &quot;banana&quot;, &quot;apple&quot;, &quot;raspberry&quot;); $fruit = array_pop($stack); print_r($stack); ?> </li></ul>
  10. 10. Important Array Function: search <ul><li>array_search($array, “item1”, “item2”…); </li></ul><ul><li><?php $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); $key = array_search('green', $array); // $key = 2; $key = array_search('red', $array);   // $key = 1; ?> </li></ul>
  11. 11. Important Array Function : rand <ul><li>array_random ($array, no_of_entries); </li></ul><ul><li><?php </li></ul><ul><li>$sentence = &quot;Pick one or more random entries out of an array&quot;; </li></ul><ul><li>$input = explode(&quot; &quot;,$sentence); </li></ul><ul><li>$rand_keys = array_rand($input, 2); </li></ul><ul><li>echo $input[$rand_keys[0]] . &quot; &quot;; </li></ul><ul><li>echo &quot;<br>&quot;; </li></ul><ul><li>echo $input[$rand_keys[1]] . &quot; &quot;; </li></ul><ul><li>?> </li></ul>
  12. 12. Important Array Function : reverse <ul><li>array_reverse($array) </li></ul><ul><li><?php </li></ul><ul><li>$input = array(&quot;php&quot;, 4.0, &quot;green&quot;, &quot;red&quot;); </li></ul><ul><li>$result = array_reverse($input); </li></ul><ul><li>$result_keyed = array_reverse($input, true); </li></ul><ul><li>?> </li></ul>
  13. 13. Important Array Function : merge <ul><li>array_merge($array1, $array2…); </li></ul><ul><li><?php </li></ul><ul><li>$array1 = array(&quot;color&quot; => &quot;red&quot;, 2, 4); </li></ul><ul><li>$array2 = array(&quot;a&quot;, &quot;b&quot;, &quot;color&quot; => &quot;green&quot;, &quot;shape&quot; => &quot;trapezoid&quot;, 4); </li></ul><ul><li>$result = array_merge($array1, $array2); </li></ul><ul><li>print_r($result); </li></ul><ul><li>?> </li></ul>
  14. 14. Important Array Function: keys <ul><li>array_keys($array, param) </li></ul><ul><li><?php </li></ul><ul><li>$array = array(0 => 100, &quot;color&quot; => &quot;red&quot;); </li></ul><ul><li>print_r(array_keys($array)); </li></ul><ul><li>$array = array(&quot;blue&quot;, &quot;red&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;blue&quot;); </li></ul><ul><li>print_r(array_keys($array, &quot;blue&quot;)); </li></ul><ul><li>?> </li></ul>
  15. 15. Date in php <ul><li>Code Description </li></ul><ul><li>d Day of the month with leading zeros </li></ul><ul><li>D Day of the week as a three-letter abbreviation </li></ul><ul><li>F Name of the month </li></ul><ul><li>h Hour from 01to 12 </li></ul><ul><li>H Hour from 00 to 23 </li></ul><ul><li>g Hour from 1 to 12(no leading zeroes) </li></ul><ul><li>G Hour from 0 to 23(no leading zeroes) </li></ul><ul><li>i Minutes </li></ul><ul><li>j Day of the month with no leading zeroes </li></ul><ul><li>l Day of the week </li></ul><ul><li>m Month number from 01 to 12 </li></ul><ul><li>M Abbreviated month name (Jan, Feb…) </li></ul><ul><li>n Month number from 1 to 23(no leading zeroes) </li></ul><ul><li>s Seconds 00 to 59 </li></ul><ul><li>S Ordinal suffix for day of the month (1st, 2nd, 3rd) </li></ul><ul><li>y Year as two digits </li></ul><ul><li>Y Year as four digits </li></ul><ul><li>z Day of the year from 0 to 365 </li></ul>
  16. 16. Some Date examples <ul><li><?php // Assuming today is: March 10th, 2008, 5:16:18 pm $today = date(&quot;F j, Y, g:i a&quot;); // March 10, 2008, 5:16 pm $today = date(&quot;m.d.y&quot;); // 03.10.08 $today = date(&quot;j, n, Y&quot;); // 10, 3, 2008 $today = date(&quot;Ymd&quot;);  // 20080310 $today = date('h-i-s, j-m-y, it is w Day z ');  </li></ul><ul><li>$today = date('i  is  he jS day.');   // It is the 10th day. $today = date(&quot;D M j G:i:s T Y&quot;); $today = date('H:m:s m is mo h'); $today = date(&quot;H:i:s&quot;); // 17:16:17 ?> </li></ul>
  17. 17. Important Debugging Functions <ul><li>print_r </li></ul><ul><ul><li>Prints human-readable information about a variable </li></ul></ul><ul><li>var_dump </li></ul><ul><ul><li>Dumps information about a variable </li></ul></ul><ul><li>die() or exit() </li></ul><ul><ul><li>Dumps information about a variable </li></ul></ul>
  18. 18. The more you dig in the more you find out <ul><li>Find out other built in functions in PHP yourself. </li></ul><ul><li>Search at www.php.net for more. </li></ul>
  19. 19. Questions <ul><li>Put forward your queries. </li></ul>
  20. 20. Lets get rolling <ul><li>Print today’s date like Today is: December 18, 2008 and time is 12:10:10 PM. </li></ul><ul><li>Reverse this sentence $str= “PHP learn to want I “ into a meaningful sentence, with use of array_reverse. </li></ul><ul><li>“ Learning PHP is not that easy and not that difficult” – print it in lower and upper case with its string length. </li></ul>

×