Home arrow PHP arrow Page 2 - String Theory

Secret Agent Man - PHP

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.

TABLE OF CONTENTS:
  1. String Theory
  2. Secret Agent Man
  3. Running Backwards
  4. Getting Into Position
  5. Instant Paralysis
  6. A Quick Trim
  7. Working The Web
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 13
September 20, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

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

<? // multi-line block $html_output = <<<EOF <html> <head></head> <body> <ul> <li>vanilla <li>chocolate <li>strawberry </ul> </body> </html> EOF; ?>
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.

<? // string print "Jeepers Creepers"; // string variable $film = "Jeepers Creepers"; print $film; ?>
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:

<? $author = "Shakespeare"; echo $author; ?> <?=$author?>


 
 
>>> More PHP Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: