PHP Strings

category: Basics
Created by: Dr.Ferrous

PHP Strings

category: Basics
Created by: Dr.Ferrous
twitter google+ facebook pinned 

Building strings

A string is simply a quoted chunk of characters: letters, numbers, spaces, punctuation, and so forth. Strings can be contained within either single or double quotation marks, and in a HEREDOC construction.
These are all strings:   "Web Courses",   'phpwin.org',   "1976",   '$5, 78%',   'August, 23, 2011'.

To make a string variable, assign a string value to a valid variable name:

You must use the same type of quotation mark for the beginning and the end of the string. You can add simple quote in a string delimited by double quotes and vice versa, double quotes when you use single-quote bookends.
If you want to add the same quote mark within a string, it must be escaped (preceding it with a backslash (\)). This keeps PHP from interpreting the second quotation mark as the end of the string.
- Double-quote bookends allow you to escape a whole list of characters:

  • \n - new line
  • \t - tab
  • \r - carriage return
  • \\ - backslash
  • \$ - dollar sign
  • \" - double quote

The "\n" character combination is seen by PHP as a new line directive when written within a double-quoted string (this does not work the same way with single quotes).

To print out the value of a string, use either echo or print.
To print the value of a variable within a context, use double quotation marks. If you add a variable inside simple quotation marks, will display its name (not its value).

Another way to build a string is to use a construct called HEREDOC. It's used to build longer strings, and therefore makes them more readable to the programmer.

Begin the HEREDOC with three less than (<) signs followed by a name designation for that string. After the string is complete, repeat the name designation on its own line with a terminating semicolon. Here is an example:

PHP has no set limits on how big a string can be.

Concatenating Strings

Concatenation is used to join strings. It's performed using the concatenation operator (.).

You see that we used the concatenation operator (.) three times. The second (.) is used to insert a comma and a space character ( ', ' ), to separate the value of the two variables.
If you are concatenating one value to another, you can use the concatenation assignment operator ( .= ).

Some PHP function for strings

PHP has a lot of useful string-specific functions. Here are just some examples:

trim("string", 'character') - Strip whitespace (or other characters, specified at 'character') from the beginning and end of a string. Without the second parameter, trim() will strip ordinary white space.

strlen("string") - Returns the length of a string.

str_word_count("string") - Counts the number of words inside "string".

ucwords("string") - Returns a string with the first character of each word in "string" capitalized.

strtolower("string") - Returns "string" with all alphabetic characters converted to lowercase.

stristr("string", 'needle') - Return the portion of the string from the beginning of the 'needle' to the end of the "string". If the 'needle' is not found, False is returned. 'needle' and "string" are examined in a case-insensitive manner.

stripos("string", 'needle') - Returns the numeric position of the first occurrence of 'needle' in the "string". If the 'needle' is not found, False is returned. 'needle' and "string" are examined in a case-insensitive manner.

strip_tags("string") - Removes embedded HTML tags from within "string".

str_ireplace('search', 'replace' "subjec") - Replace all occurrences of the 'search' string with the 'replace' string in "subject" (case-insensitive).