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

More Related Content

PPT
Php array
PPT
Php Using Arrays
PPT
PDF
Php array
PPT
Class 4 - PHP Arrays
PPTX
Array in php
PDF
Arrays in PHP
PDF
4.1 PHP Arrays
Php array
Php Using Arrays
Php array
Class 4 - PHP Arrays
Array in php
Arrays in PHP
4.1 PHP Arrays

What's hot (18)

PPTX
PHP Functions & Arrays
PPTX
Chap 3php array part1
PPTX
PHP array 1
PDF
PHP Unit 4 arrays
PDF
Sorting arrays in PHP
PDF
DBIx::Class introduction - 2010
PPTX
Marcs (bio)perl course
PDF
DBIx::Class beginners
PDF
Working with text, Regular expressions
ODP
Introduction to Perl
PDF
Scripting3
ODP
Intermediate Perl
PDF
Climbing the Abstract Syntax Tree (php[world] 2019)
PPT
LPW: Beginners Perl
KEY
Introduction to Perl Best Practices
PHP Functions & Arrays
Chap 3php array part1
PHP array 1
PHP Unit 4 arrays
Sorting arrays in PHP
DBIx::Class introduction - 2010
Marcs (bio)perl course
DBIx::Class beginners
Working with text, Regular expressions
Introduction to Perl
Scripting3
Intermediate Perl
Climbing the Abstract Syntax Tree (php[world] 2019)
LPW: Beginners Perl
Introduction to Perl Best Practices
Ad

Viewers also liked (6)

PDF
PHP Basic & Arrays
PPTX
Array in php
ODP
My self learn -Php
PDF
Web app development_php_06
PPTX
Php string function
PHP Basic & Arrays
Array in php
My self learn -Php
Web app development_php_06
Php string function
Ad

Similar to 03 Php Array String Functions (20)

ODP
Intro to #memtech PHP 2011-12-05
ODP
PHP 102: Out with the Bad, In with the Good
PPT
Php Crash Course
PPTX
PPT
Php Calling Operators
PPT
Dealing with Legacy Perl Code - Peter Scott
PPT
Web Scraping with PHP
PPT
Php Basic Security
PPT
JSP Custom Tags
ODP
Zendcon 2007 Features
PPT
Php Training
PPT
Forum Presentation
PPT
Introduction To Lamp
ODP
Haml & Sass presentation
ODP
PHP Basics for Designers
PPT
Exploiting Php With Php
PPT
PPT
Basic PHP
ODP
Terms of endearment - the ElasticSearch Query DSL explained
Intro to #memtech PHP 2011-12-05
PHP 102: Out with the Bad, In with the Good
Php Crash Course
Php Calling Operators
Dealing with Legacy Perl Code - Peter Scott
Web Scraping with PHP
Php Basic Security
JSP Custom Tags
Zendcon 2007 Features
Php Training
Forum Presentation
Introduction To Lamp
Haml & Sass presentation
PHP Basics for Designers
Exploiting Php With Php
Basic PHP
Terms of endearment - the ElasticSearch Query DSL explained

More from Geshan Manandhar (20)

PDF
How to build an image to geo location guesser using Gemini 2 - Canberra.pdf
PDF
build-with-ai-sydney AI for web devs Tamas Piros
PDF
Are logs a software engineer’s best friend? Yes -- follow these best practices
PDF
We lost $ 20.5K in one day and how we could have saved it… hint: better autom...
PDF
Moving from A and B to 150 microservices, the journey, and learnings
PDF
Adopt a painless continuous delivery culture, add more business value
PDF
Things i wished i knew as a junior developer
PDF
Embrace chatops, stop installing deployment software - Laracon EU 2016
PDF
Do You Git Your Code? Follow Simplified Gitflow Branching Model to Improve Pr...
PDF
Embrace chatOps, stop installing deployment software
PDF
7 rules of simple and maintainable code
PDF
Software engineering In Nepal mid 2015 part 01
PDF
A simplified Gitflow
PDF
How to become a better software company technically
PDF
Things I wished I knew while doing my tech bachelor / undergraduate
PDF
Message Queues a basic overview
PDF
Most popular brands, people on facebook in nepal as of 2013 q4
PDF
Drupal 7 basic setup and contrib modules for a brochure website
PPTX
Git intro hands on windows with msysgit
ODP
Drupal 7 install with modules and themes
How to build an image to geo location guesser using Gemini 2 - Canberra.pdf
build-with-ai-sydney AI for web devs Tamas Piros
Are logs a software engineer’s best friend? Yes -- follow these best practices
We lost $ 20.5K in one day and how we could have saved it… hint: better autom...
Moving from A and B to 150 microservices, the journey, and learnings
Adopt a painless continuous delivery culture, add more business value
Things i wished i knew as a junior developer
Embrace chatops, stop installing deployment software - Laracon EU 2016
Do You Git Your Code? Follow Simplified Gitflow Branching Model to Improve Pr...
Embrace chatOps, stop installing deployment software
7 rules of simple and maintainable code
Software engineering In Nepal mid 2015 part 01
A simplified Gitflow
How to become a better software company technically
Things I wished I knew while doing my tech bachelor / undergraduate
Message Queues a basic overview
Most popular brands, people on facebook in nepal as of 2013 q4
Drupal 7 basic setup and contrib modules for a brochure website
Git intro hands on windows with msysgit
Drupal 7 install with modules and themes

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
A Presentation on Artificial Intelligence
PDF
Machine learning based COVID-19 study performance prediction
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Cloud computing and distributed systems.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Approach and Philosophy of On baking technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
cuic standard and advanced reporting.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
A Presentation on Artificial Intelligence
Machine learning based COVID-19 study performance prediction
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Cloud computing and distributed systems.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Approach and Philosophy of On baking technology
The AUB Centre for AI in Media Proposal.docx
Understanding_Digital_Forensics_Presentation.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
MYSQL Presentation for SQL database connectivity
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Review of recent advances in non-invasive hemoglobin estimation
Spectral efficient network and resource selection model in 5G networks
Building Integrated photovoltaic BIPV_UPV.pdf

03 Php Array String Functions

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