Well, day 7 here, and it’s time for Strings. This is a long chapter, and a lot to blog about. So I estimate 2-3 days on this topic. Plus I don’t have a lot of time tonight to write a lot.
- There are two types of strings in PHP, simple strings and complex strings. Simple strings contain simple, raw, text. Complex strings can have escape sequences (new lines, tabs, special characters) or variables embedded in the string itself. Simple strings are represented by ‘single quotes.’ And complex strings by "double quotes."
$test = “Fiber”;
-
echo "Carbon \t $test"; // outputs "Carbon Fiber"
-
echo 'Carbon \t $test'; // outputs "Carbon \t $test"
-
- Escape sequences are preceded with a backslash and then one or more characters. The most common are \n for a newline and \t for a tab. There can also be character representations with hex code by using \x00 where 00 is replaced with some hex code. And the same with octal by \055 where 55 is replaced with some octal code.
- There are a couple ways to include a variable in a complex string. The simplest is by inserting the variable name directly: echo "$num"; A problem can occur when there are some characters immediately after the variable: echo "$nums"; This will try to include the variable $nums, when in-fact you want $num . "s" The solution here is to use curly braces "{}" around the variable: echo "{$num}s"; This is also useful if you want to access a specific index of an array: echo "{$list[12]}";
- While double quotes are normally used to create complex strings, you can also use Heredoc. This involves three left angle brackets "<<<" followed by a closing word, a new line, all the content you want to include (new lines allowed), another new line, your closing word, a semicolon. Here's an example:
echo <<
-
This content
-
can be long,
-
can have returns
-
can include escape sequences,\n
-
and can utilize $variables.
-
EOL;
-
-
$test = <<<WOOHOO
-
This is fun!
-
WOOHOO;
- Since we use ", ‘, $ and \ as special characters in strings, sometimes we want to display them as normal. We can escape these characters by using a backslash (\). Example:
echo “I want \$a, \”quote,\” and some \\s”; // Displays: I want $a “quote,” and some \s-
echo 'I need to escape \'this\' or else.'; // Displays: I need to escape 'this' or else
- String length is determined via the strlen() function (which is binary safe). Just as we access various indexes in Arrays with the square brackets ([]), we can access characters from strings using the same concept. Note: strings are zero-based index, just like arrays.
$string = “abc”;-
echo $string[1]; // b
- If you wanted to replace various characters in a string with alternate ones, you can use the strtr($string, $from, $to) (translation) function. It replaces all instances of $from in $string with $to. It can also be done with arrays and key/value pairs instead of three parameters.
$replace = array(“a” => “b”, “p” => “q”);-
echo strtr("apple", $replace); // Displays: bqqle
The next section is all about comparing, searching, and replacing strings, of which I don’t have time for tonight (sorry). So, goodnight and good luck!
No Comments -

