PHP
  Home arrow PHP arrow Page 2 - String Theory
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

String Theory
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2001-09-20

    Table of Contents:
  • String Theory
  • Secret Agent Man
  • Running Backwards
  • Getting Into Position
  • Instant Paralysis
  • A Quick Trim
  • Working The Web

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    String Theory - Secret Agent Man


    (Page 2 of 7 )

    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


     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway