Friday, 14 August 2015

PHP Strings :-
  • They are sequences of characters, like "String operations in PHP".
  • Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.
  • Let us consider an example:-
          strings.php:-
          <html>
          <body>
          <?php
           $x="Hi";

           print("<b><i>Singly Quoted Strings</i></b><br/>");
           $y='<b> $x </b>, How are U!';
           print($y); print("<br/><br/>");

           print("<b><i>Doubly Quoted Strings</i></b><br/>");
           $y="<b> $x </b>, How are U!";
           print($y);
           ?>
          </body>
          </html>

          Screenshot:-
          

             
            Here, $x is printed as it is, when it is declared in single-quote(' '). 
            The value of the variable $x is evaluated when it is declared in double-quote(" ").

  • To concatenate two string variables together, we need to use the dot (.) operator.
  • Consider an example:-
          concatenate.php:-
          <html>

          <body>
          <?php
            $string1 = "Hello, ";
            $string2 = "Learning PHP...!!!";
            echo "First String :- <b> $string1 </b></br/>";
            echo "Second String :- <b> $string2 </b><br/><br/>";
            echo "<b><i> After Concatenation, by using dot(.) operator </i></b><br/>";
            echo $string1 . " " .$string2;
           ?>
          </body>
          </html>

          Screenshot:-
          
            
         
  • strlen() function can be used in PHP, to find the length of a string.
  • Consider an example:-
          strlen.php:-
          <html>

          <body>
          <?php
           echo "String used :-<b> <i> Hi Everyone </i> </b><br/><br/>";
           echo "The length of the String Is :- <br/>";
           echo "<b>";
           echo strlen("Hi Everyone");
           echo "</b>";
           ?>
           </body>
           </html>

          Screenshot:-
          
  • The strpos() function is used to search for a string or character within a string.
  • If a match is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE.
  • Consider an example:-
          strpos.php:-
          <html>
          <body>
          <?php
           echo "String used :-<b> <i> Hi Everyone </i> </b><br/><br/>";
           echo "The Position of the String(Every) Is :- <br/>";
           echo "<b>";
           echo strpos("Hi Everyone","Every");
            echo "</b>";
            ?>
           </body>
           </html>

           Screenshot:-
           

            Here, the 'w' character of the 'world' string is at position 7, but it shows the position at 6
            It is so as the first position in the string is 0 and not 1.

No comments:

Post a Comment