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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT