You may not know this, but the latest version of PHP comes with avery powerful set of string manipulatation tools. This article takes anin-depth look at these tools and illustrates how they can save you time andeffort in your daily development activities.
We'll begin right at the top, with some very basic definitions and concepts.
In PHP, the term "string" refers to a sequence of characters. The following are all valid examples of strings:
"ciao"
"I ROCK!"
"a long time ago in a galaxy far, far away"
String values can be assigned to a variable using the
standard assignment operator.
<?
$identity = "Spiderman";
?>
String values may be enclosed in either double quotes ("") or
single quotes('') - the following variable assignments are equivalent"
<?
$car = "Porsche";
?>
<?
$car = 'Porsche';
?>
String values enclosed in double quotes are automatically
parsed for variable names; if variable names are found, they are automatically replaced with the appropriate variable value.
<?
$identity = "James Bond";
$car = "BMW";
// this would contain the string "James Bond drives a BMW"
$sentence = "$identity drives a $car";
?>
PHP also allows you to create strings which span multiple
lines. The original formatting of the string, including newlines and whitespace, is retained when such a string is printed.
The <<< symbol indicates to PHP that what comes next
is a multi-line block of text, and should be printed as is right up to the marker "EOF". In PHP-lingo, this is known as "here document" syntax, and it comes in very handy when you need to output a chunk of HTML code, or any other multi-line string.
Strings can be concatenated with the string concatenation operator, represented by a period(.)
<?
// set up some string variables
$a = "the";
$b = "games";
$c = "begin";
$d = "now";
// combine them using the concatenation operator
// this returns "the games begin now"
$statement = $a . " " . $b . " " . $c . " " . $d;
// and this returns "begin the games now!"
$command = $c . " " . $a . " " . $b . " " . $d . "!";
// this also returns "begin the games now!"
$command = "$c $a $b $d!";
?>
Note that if your string contains quotes, carriage returns or
backslashes, it's necessary to escape these special characters with a backslash.
<?
// will cause an error due to mismatched quotes
$film = 'America's Sweethearts';
// will be fine
$film = 'America\'s Sweethearts';
// will generate an error
$story = "...and so he said, "backslash me, knave!"";
// will be fine
$story = "...and so he said, \"backslash me, knave!\"";
?>
The print() function is used to output a string or string
variable.
PHP also offers the echo() construct, which does the same
thing.
<?
// string
echo "Shakespeare";
// string variable
$author = "Shakespeare";
echo $author;
// combine the two
echo "Despite what critics may say, $author's influence can be felt even
today";
?>
Since displaying variable values is one of the most
fundamental things you can do, PHP also offers a shortcut syntax (similar to that offered by JSP) to simplify this task. The following two statements are equivalent: