module4-Strings.pdf - CS 85 PHP PROGRAMMING Module 4...

This preview shows page 1 - 4 out of 19 pages.

Module 4: Strings Working with Strings Parse Stings Compare Stings Single and multiple strings CS 85: PHP PROGRAMMING Santa Monica College Computer Science & Information Systems Dept.
PHP is an extremely powerful tool for generating HTML code as well as processing user submitted data. HTML code and use data is commonly in a string format. This module will cover how to format and manipulate strings. By using built-in PHP functions and regular expressions a PHP developer can perform such task as comparing strings, match and replacing substrings, joining or splitting strings. What is a String? A string is any sequence of characters, like "Hello world!" , ‘a’, “123”, “a1b2c3”, $singleVar, etc . A string can either be a text string surrounded by either single or double quotes. A variable with a string assigned to it can also be considered a string, because it stores a string. When working with strings, it is important to remember all strings must be enclosed in a single or double quote and the ending quote and must match the beginning quote. If for example you want to the include a single or double quote in the string, the opposite quotes should be used to declared the string. For example $myQuote = ‘<p>” Never on Tuesday … “<p>’; String Operators There are two string operators in PHP. The first string operator is the concatenation operator ('.') a single period, which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.=') is the period and equal symbol, which appends the string on the right side to the string on the left side. The string in these examples can be a variable with a string value or the actual string. <?php $a = "Hello " $b = $a . "World!" ; // now $b contains "Hello World!" $a = "Hello " $a .= "World!" ; // now $a contains "Hello World!" ?> Escape Characters When working with special characters in PHP strings, special care has to be taken to ensure the string is interpreted by the PHP engine correctly. If a special character is added to a strings, unusual behavior or fetal execution errors might occur. For example, this line of code will have unexpected behavior. The single quote in the word “it’s” will prematurely end the strings, whe n the PHP interpreter match the beginning single quote to the “it’s” word single quote. Upon execution of this script, a parse error will occur. ; ; //Special character caused script to error out
echo ‘It’s your birthday!’; By using escape characters in your string, it flags the PHP interpreter that the character that follows the escape character serves a special purpose. In PHP the escape character is the backslash (\). By using the backslash in front of a special character such a single q uote (‘) or dollar s ign ($) symbol, is telling the PHP interpreter to treat the single quote or dollar symbol as normal characters and not as the beginning or ending of a PHP string or in the case of the dollar symbol ($), not a variable but only as the text symbol of a dollar sign. //Special Character with Escape Character echo ‘It \ ’s your birthday!’;

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture