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)

PDF
Php tips-and-tricks4128
PPTX
Php functions
PDF
PHP tips and tricks
PPTX
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
PPTX
UNIT IV (4).pptx
PPT
Arrays in php
PPT
PHP-04-Arrays.ppt
ODP
Php Learning show
PPT
PHP and MySQL with snapshots
PPT
P H P Part I, By Kian
PPTX
Chapter 2 wbp.pptx
PDF
lab4_php
PDF
lab4_php
PDF
How to run PHP code in XAMPP.docx (1).pdf
PPSX
DIWE - Advanced PHP Concepts
PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
PPT
Php tips-and-tricks4128
Php functions
PHP tips and tricks
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
UNIT IV (4).pptx
Arrays in php
PHP-04-Arrays.ppt
Php Learning show
PHP and MySQL with snapshots
P H P Part I, By Kian
Chapter 2 wbp.pptx
lab4_php
lab4_php
How to run PHP code in XAMPP.docx (1).pdf
DIWE - Advanced PHP Concepts
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv

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
NewMind AI Weekly Chronicles – August ’25 Week IV
PPTX
Module 1 Introduction to Web Programming .pptx
PDF
Lung cancer patients survival prediction using outlier detection and optimize...
PDF
Co-training pseudo-labeling for text classification with support vector machi...
PDF
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
PDF
EIS-Webinar-Regulated-Industries-2025-08.pdf
PDF
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
PDF
Human Computer Interaction Miterm Lesson
PDF
Auditboard EB SOX Playbook 2023 edition.
PDF
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
PDF
substrate PowerPoint Presentation basic one
PDF
5-Ways-AI-is-Revolutionizing-Telecom-Quality-Engineering.pdf
PDF
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
PDF
Ensemble model-based arrhythmia classification with local interpretable model...
PDF
Data Virtualization in Action: Scaling APIs and Apps with FME
PPTX
MuleSoft-Compete-Deck for midddleware integrations
PPTX
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
PDF
zbrain.ai-Scope Key Metrics Configuration and Best Practices.pdf
PDF
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
PDF
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
NewMind AI Weekly Chronicles – August ’25 Week IV
Module 1 Introduction to Web Programming .pptx
Lung cancer patients survival prediction using outlier detection and optimize...
Co-training pseudo-labeling for text classification with support vector machi...
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
EIS-Webinar-Regulated-Industries-2025-08.pdf
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
Human Computer Interaction Miterm Lesson
Auditboard EB SOX Playbook 2023 edition.
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
substrate PowerPoint Presentation basic one
5-Ways-AI-is-Revolutionizing-Telecom-Quality-Engineering.pdf
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
Ensemble model-based arrhythmia classification with local interpretable model...
Data Virtualization in Action: Scaling APIs and Apps with FME
MuleSoft-Compete-Deck for midddleware integrations
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
zbrain.ai-Scope Key Metrics Configuration and Best Practices.pdf
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.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.