PHP - String

String Variables are used to store set of characters enclosed with single (') or double (") quotes. In this post covering general operations and operations of string.
Ex:
  <?php
      $str = "PHP programing with String"; // Assign characters to string variable
      echo $str; // Print the string
  ?>

Out put: 
PHP programing with String


  • String Concatenation
    You can put two or more strings together with (.) operator.
    Ex: 
      <?php
        $str1 = "PHP Programing";
        $str2 = " with String";
        echo $str1 . $str2;
      ?>

    Out put: 
    PHP programing with String

    Or
    Ex: 
      <?php
        $str1 = "PHP Programing";
        $str2 = "String";
        echo $str1 . " with ". $str2; // Concatenate two variables with a set of characters
      ?>

    Out put: 
    PHP programing with String

  • String with a Line break
    You can put line breaks in two ways
    • Normal HTML  tag
      <?php
          echo "This is the 1st Line <br />";
          echo "This is the 2nd Line ";
      ?>

      Out put: 
      This is the 1st Line
      This is the 2nd Line

    • php special characters
      <?php
          print "This is the 1st Line \n
      This is the 2nd Line";
      ?>

      Out put: 
      This is the 1st Line
      This is the 2nd Line

  • PHP Special Characters
    • \n - to print a new line
    • \' - to print single quote 
    • \" - to print double quotes
    • \\ - to Print a back slash
    • \$ - to print ($) sign

No comments:

Post a Comment