Monday, 20 May 2013

string in php


PHP String:

    String is a type in PHP and it is a series of characters. There are exactly 256 different characters possible. This also implies that PHP has no native support of Unicode.
    However I have a good news as well as there is no limitation in string length in PHP (Of course available memory and PHP ini settings can reduce it).
    You can set a string in three different ways:
    1- The first and most known way is to specify a string in single quotes. Similar to other programing languages you need to use backslash '\' if you want to display a single quote in your text.

    php code
    <?php $str_1 = 'Demo text'; $str_2 = 'Demo text with single quote'; echo $str_1; echo $str_2; ?>
    Output
    Demo text
    Demo text with single quote 

    2- The second option to use double quotes. Between double quotes you can use more escape character in your string. Besides this if you put a variable in the string then PHP will interpret it and display the content of the variable.

    php code
    <?php $demo = "Internal"; $str_3 = "This is a $demo string"; echo $str_3; ?>
    Output
    This is a Internal string 

    3- And last you can use heredoc as well. In this case you define your text between heredoc identifiers. In this case it is DEMO. You need to start it with the operator <<< and then the identifier. If you are ready you need to close the heredoc part by adding the identifier again at the beginning of a new line like this:

    php code
    <?php $str_1 = &lt;&lt;&lt;DEMO another string declaration. DEMO; echo $str_1; ?>
    Output
    another string declaration. 

    PHP String:Functions

      Some Usefull String Functions

      FunctionDescryption
      empty()Addition
      strlen()Substraction
      strrev()Multiplication
      strtoupper()Division
      strtolower()Modulus
      str_repeat()Repeat a string to a given specified number of time.
      str_word_count()Output the number of word in string.
      strcmp()compare two strings.
      substr()to extract a substring from a given string.
      trim()removes the characters and white space from a string.
      ucfirst()converts first character of a string into uppercase.
      ucword()converts first character of each word of a string into uppercase.
      str_replace()repalce characters in a string(case-sensitive).
      str_ireplace()repalce characters in a string(case-insensitive).

No comments:

Post a Comment