Jumat, 03 Januari 2014

PHP substr(), strpos(), strlen(), strrev()

Now we going to discuss about the use of 'substr', 'strpos', 'strlen', and 'strrev' in PHP

substr - return part of a string

   
   <php
      $string = "I Love PHP";
      $result = substr($string, 0, 1);    // returns "I"
      $result = substr($string, 2, 4);    // returns "LOVE"  
      $result = substr($string, -3);      // returns "PHP"
   ?>

strpos - find the numeric offset position

   
   <php
      $string = "I Love PHP";
      $result = strpos($string, "I");    // returns "0"
      $result = strpos($string, "L");    // returns "2"  
      $result = strpos($string, "P");    // returns "7"
      /*to know the next position of the same string you must give an offset higher than previous offset position*/
      $result = strpos($string, "P", 8); // returns "9"
   ?>

strlen - count the number of character in string (space included)

   
   <php
      $string = "I Love PHP";
      $result = strlen($string);         // returns "10"
   ?>

strrev - read string from end to start

   
   <php
      $string = "I Love PHP";
      $result = strrev($string);         // returns "PHP evoL I"
   ?>

PHP Hello World

Before learning php furthermore, the first thing to be done by the php programmers are showing words Hello World on the browser using php programming language

Method 1

   
   <?php
      echo "Hello World";
   ?>
save as hello1.php on your localhost (if you using XAMPP save it on folder xampp/htdocs) now, you can view your script on your browser http://localhost/hello1.php

Method 2

   
   <?php
      $string = "Hello World";
      echo $string;
   ?>
save as hello2.php on your localhost. now, check it out in your browser http://localhost/hello2.php
each method will produce the "Hello World" on browser.