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 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.

images/articles/php/php-strings.jpg

In PHP, strings are often delimited by single quotes.

$a = 'Hello';

String Functions

  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. str_replace: This function replaces all occurrences of a given string by the replacement string.

  5. substr: This function extracts the string contained between the positions specified by parameters, with the first character being at position 0.

  6. strpos: This function shows the position of the first occurrence of the given string. It returns false if the string cannot be found.

  7. 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.

When using the single quote, the only escape characters that work are the 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: 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"

PHP 7 introduced the Unicode escape character, which provides the ability to embed UTF-8 encoded characters into strings. Such a character is specified as a hexadecimal
number inside curly brackets. The number can be up to six digits long, with leading zeros being optional.

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

How to Join Strings / String Concatenation

You can join strings, a process called concatenation, by using a dot (.). The dot symbol is known as the concatenation operator (.). 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