Day 7 – Strings

Zend PHP Certification

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”;
    1. echo "Carbon \t $test"; // outputs "Carbon       Fiber"
    2. 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 <<
    1. This content
    2. can be long,
    3. can have returns
    4. can include escape sequences,\n
    5. and can utilize $variables.
    6. EOL;
    7.  
    8. $test = <<<WOOHOO
    9. This is fun!
    10. 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
    1. 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”;
    1. 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”);
    1. 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

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



  • Donate

    If my work has helped you and you want to return the favor, you could purchase something for me from my Amazon Wish List or send me a donation via PayPal.

  • License

    Unless otherwise noted, all source code and compiled files published on this website are released under the terms of the GNU Lesser General Public License.