PHP Strings

Strings are concatenations or series of characters that you cannot see at once when searching for something. Instead, you have to look one by one and keep track of what the content is.

PHP Strings

PHP has many predefined functions that help you in interacting with strings. Characters are letters, numbers, and punctuation. When a number is used as a character, it is just a stored character, the same as a letter. It can't be used in arithmetic.

In PHP, strings are often delimited by single quotes.

$a = 'Hello';

Character positions in PHP are numbered starting from zero. Thus, the character at position 0 is H, at position 1 is e, and so on.

String Functions

PHP has more than 100 built-in functions for manipulating and analyzing strings, ranging from standard tasks like toggling between uppercase and lowercase letters to more specialized tasks such as implementing hash algorithms.

  1. strlen(): This function returns the number of characters (length) that the string contains.
  2. trim(): This function returns the string, removing all the blank spaces to the left and to the right.
  3. strtoupper() and strtolower(): These functions return the string with all the characters in upper or lower case respectively.
  4. ucfirst(): It capitalizes just the first letter of a string.
  5. ucwords(): It capitalizes the first letter of every word in a string.
  6. str_replace(): This function replaces all occurrences of a given string by the replacement string.
  7. substr(): This function extracts the string contained between the positions specified by parameters, with the first character being at position 0.
  8. strpos(): This function shows the position of the first occurrence of the given string. It returns false if the string cannot be found.
  9. substr_count(): This function counts the number of times a substring appears within a string.
  10. Additionally, there is an operator for strings (.) which concatenates two strings.

Single Quotes or Double Quotes

You can enclose strings within single quotes or double quotes. The difference is that within single quotes, a string is exactly as it is represented, but within double quotes, some rules are applied before showing the final result. There are two elements that double quotes treat differently than single quotes: escape characters and variable expansions.

Escape Characters

These are special characters than cannot be represented easily. Examples of escape characters are new lines or tabs. To represent them, you use escape sequences, which are the concatenation of a backslash (\) followed by some other character. For example, \n represents a new line, and \t represents a tabulation.

PHP has just two special cases for single-quoted strings. First, since single quotes serve to delimit the string, there must be a mechanism for including a single quotation mark inside the string itself. Otherwise, the single quote will be interpreted as the end of the string. The solution is to put a backslash in front of the single quote (\').

Since the backslash is needed to escape single quotation marks, there must also be a way to specify a backslash character in a single-quoted string. For that, write two backslashes: \\.

So, the only escape characters that work are backslash (\\) and single-quote (\') characters. Escaping the backslash is only necessary before a single quote or at the end of the string.

$s = 'It\'s'; // It's

Variable Expanding

Double-quoted strings differ from their single-quoted counterparts in that they are parsed, or processed, by the PHP, which means they can include variables. This allows you to include variable references inside the string, and PHP replaces them by their current value. You have to include the $ sign too.

$c = 'World';
echo "Hello $c"; // "Hello World"
echo 'Hello $c'; // "Hello $c"

Unicode Characters

Not all characters are available to type directly from your keyboard. Unicode is an international standard for declaring and working with a wide range of characters and symbols, including ordinary English letters, emojis, letters from other alphabets, and more. Each Unicode character is defined by a unique hexadecimal code. For example, the code 1F60A corresponds to one of several smiling emojis.

To use a Unicode character in a double-quoted string, start with the escape sequence \u, then add character's hexadecimal code in curly brackets.

echo "\u{00C2A9}"; // © (copyright sign)
echo "\u{C2A9}"; // ©

How to Join Strings? String Concatenation

You can join strings, variables, or constants to form a single string expression. You can do this with string concatenation operator, represented by a dot or period (.). It combines two strings into one. It also has an accompanying assignment operator (.=), which appends the right-hand string to the left-hand string variable.

$b = $a . ' World'; // Hello World
$a .= ' World'; // Hello World