·  PHP – Syntax – Variables – String

 

- Info:

  - PHP has only one type of variable, called scalar.

  - This tutorial shows possible ways of loading scalar with constant string.

  - If string can't be interpreted as number it must be enclosed between single or double qoutes.

 

- Create – Using Literal:

  - String can be created by simply defining it using single or double qoutes:

    $string = 'Test';

    $string = "Test";

  - String can be display by using one of the following functions (among others defined later):

    print($string);

    echo ($string);

 

- Create – Using Single Qoutes:

  - If single-qouted string contains variable name it will not be replaced with variable value.

    $var    = 'Test';

    $string = 'Variable name is $a.'#Result is: Variable name is $a.

  - In single-qouted strings you can only escape backslash and single qoute by using '\\' or '\''.

    $string = 'Escaping \\ and \'.';   #Result is: Escaping \ and '.

 

- Create Using Double Qoutes:

  - If double-qouted string contains variable name it will be replaced with variable value.

    $var    = 'Test';

    $string = "Variable value is $a."#Result is: Variable value is Test.

 

- Create Using Heredoc:

  - Heredoc is used to create multyline strings.

  - It is indicated by placincg <<< after equal sign followed by defining arbitrary closing tag ENDTAG.

  - Defined closing tag ENDTAG; must be placed at the end of multy line string without indentation.

    $string = <<< ENDTAG

    First line

    Second line

    ENDTAG;

 

- Create From Array:

  - Create string from array elements:

    $array  = array('A', 'B', 'Last letter'=>'C');

    $string = implode($array);       //ABC     Create string from array values.

    $string = implode($array, "::"); //A::B::C Use 2nd string in between as glued between array values.

 

- Display:

  - Following functions can be used to display strings:

    echo  ("Hello");

    print ("Hello");

    printf("Hello");

 

- Concatenate:

  - You can connect strings using double qoutes, '.' or '.=':

    $a      = "Hello";

    $b      = "World";

    $string = $a." ".$b//Hello World

    $string = "$a $b";    //Hello World

    $a     .= $b;         //Hello World Same as $a=$a.$b

 

- Analyze:

  - Returns number of characters in string:

    $length = strlen  ("Hello");           //5  Number of characters in string. 0 if empty.

  - Returns number of words in a string:

    $count  = str_word_count("Hi & all"         ); //2 Number of words in string. '&' isnt seen as word.

    $count  = str_word_count("Hi & all"  ,0     ); //2 Same as above. 0 means return number of words.

    $count  = str_word_count("Hi & # all",0,"&#"); //4 Count '&' and '#' as words.

    $array  = str_word_count("Hi all"    ,1);      //{Hi,all}       1 gives array of words.

    $array  = str_word_count("Hi all"    ,2);      //{0=>Hi,3=>all} 2 gives hash of words.Key is index

  - Returns position of second string in first string:

    $index  = strpos  ("HelpHelp","He"  ); //0  Index of first occurence of "He". NULL is not found.

    $index  = strpos  ("HelpHelp","He",3); //4  Index of first occurence of "He" starting from 3.

    $index  = stripos ("HelpHelp","HE"  ); //0  Like strpos() but case insensitive.

   

    $index  = strrpos ("HelpHelp","He"  ); //4  Index of last occurence of "He". NULL is not found.

    $index  = strrpos ("HelpHelp","He",3); //4  Index of last occurence of "He" starting from 3.

    $index  = strrpos ("HelpHelp","He",-5);//0  Index of last occurence starting from -5 backwards

    $index  = strripos("HelpHelp","HE"  ); //4  Like stripos() but case insensitive.

 

    if($index === false) { print("String was not found"       ); }

    if($index !== false) { print("String was found at ".$index); }

  - Returns number of characters in first string before any of the characters in second string:

    $count  = strcspn ("HelpHelp","we"  ); //1  Number of characters before any character in 2nd string

    $count  = strcspn ("HelpHelp","we",3); //2  Number of chars between index and any char in 2nd string

 

- Substrings:

  - String characters can be accessed as array elements:

    $string = "Hello";

    $char   = $string[0];               //H        Get first character from string.

  - Return substring between given indexes:

    $substr = substr ("Hello",1,3);     //ell      From index 1 return 3 characters.

  - Return substring from the first occurence of second string:

    $substr = strstr ("HelpHelp","He"); //HelpHelp Return substring from the first occurence of "He".

    $substr = stristr("HelpHelp","HE"); //HelpHelp Like strstr() but case insensitive.

    $substr = strchr ("HelpHelp","He"); //HelpHelp Like strstr().

  - Return substring from the first occurence of any char in 2nd string:

    $substr = strpbrk("HelpHelp","wp"); //pHelp    Substring from first occurence of any char in 2nd str

  - Following functions remove default characters (\0, \t, \n, \r, ' ') from both sides of the string:

    $substr = trim   (" Hello ");       //Hello    Removes default characters from both sides.

    $substr = ltrim  ("  Hello");       //Hello    Removes default characters from left side.

    $substr = rtrim  ("Hello  ");       //Hello    Removes default characters from right side.

    $substr = chop   ("Hello  ");       //Hello    Like rtrim(). Default chars: \0, \t, \n, \r, ' '.

 

    $substr = trim   (" Hello ","e o"); //Hell     Removes specified characters from both sides.

    $substr = trim   ("  Hello","e..o");//Hell     Removes characters which belong to specified range.

  - Split string into array using first string as separator.

    $array  = explode("--","A--B--C" ); //{A,B,C}  Split string into array around first string.

    $array  = str_word_count("Hi all",1);      //{Hi,all}       1 gives array of words.

    $array  = str_word_count("Hi all",2);      //{0=>Hi,3=>all} 2 gives hash of words.Key is index

 

- Modify:

  - String characters can be modified as array elements:

    $string    = "Hello";

    $string[0] = "A";    //Aello Replace first character with "A".

  - Replace occurences of 1st string with 2nd:

    $string    = str_replace("llo","lp","HelloHello"); //HelpHelp Replace 1st string with 2nd.