Programming Basics
  Home arrow Programming Basics arrow Page 3 - PHP Strings Primer
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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? 
PROGRAMMING BASICS

PHP Strings Primer
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2003-07-11

    Table of Contents:
  • PHP Strings Primer
  • The Basics
  • Single Quotes
  • Double Quotes
  • Heredoc
  • Concatenation
  • Displaying Strings
  • echo
  • print
  • printf
  • Strings Formatting
  • Preparing user input for comparisons
  • Capitalization
  • Reversing strings
  • Padding strings
  • Multiple Lines
  • Data Preparation
  • Adding and Removing Slashes
  • Dealing with HTML Tags and Entities
  • Counting
  • Checking password strength
  • Generating Statistics
  • Substrings (and searching)
  • Extracting Substrings
  • Counting Paragraphs
  • Filtering Words
  • Working with email addresses
  • Manually Stripping Tags
  • Password Strength Revisited
  • Handling URLs and Base64-encoding
  • Parsing URLs
  • Encoding for URLs
  • Encoding for Email
  • Hashing
  • Verifying Integrity
  • User Authentication
  • Conclusion

  • 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


    PHP Strings Primer - Single Quotes


    (Page 3 of 37 )

    The most basic form of string usage is with single quotes. Everything within the single quotes is taken as a literal. Unlike other forms of quoting, which will substitute values for variable names within a string, this scheme stores a string exactly as it is entered. Let's take a look at this basic example:

    <?php
    $mystring 
    'Hello!';
    ?>

    This assigns the literal string 'Hello!' to the variable '$mystring'. This is about as simple as it gets. As we mentioned a moment ago, this way of quoting does not provide variable substitution. Everything is taken as a literal. To illustrate this point, take a look at the following example.

    $mystring = '$var = 1;';

    This assignment would result in a string being stored in the variable $mystring just as you see it above. When we cover the double quote technique in a moment, you will see how variable substitution works.

    Continuing on with single quotes, if we want to add newlines to our string we can easily just span the string over multiple lines:

    <?php
    $mystring 
    'This is the first line,
    and this is the second.'
    ;
    ?>

    This is the only method that will work for adding newlines to a single quoted string. Attempting to include the escape sequence for a newline, as with the following example, will not work.

    <?php
    //this will not add a newline to the string
    $mystring 'First line\nSecond line';
    ?>

    If we would like to include the literal single quote character within our string, we need to escape it. Within single quotes, the only character that needs to be escaped is the literal single quote character. The one exception to this rule is for the character used to do the escaping, the backslash. If you need a backslash to appear before a single quote, or at the end of a string, you will need to double it. Examples of escaping a single quote and using backslashes are seen below. :

    <?php
    $mystring 
    'That\'s too bad.';
    $anotherstring 'This string has a backslash at the end \\';
    $andanother 'A backslash before the single quote: That\\'s to bad.';
    ?>

    Using single quotes is the most efficient method of dealing with strings because no variable substitution takes place. The speed increase won't be awesome, but every little helps. If you are using very few strings, the difference will barely be measurable. The efficiency gains really don't come into place until you are dealing with a large amount of strings.

    More Programming Basics Articles
    More By Matt Wade


       · comment
       · really goood work ,it covers all the major string functions.also explained with...
       · test'ng
     

    PROGRAMMING BASICS ARTICLES

    - PHP: Hypertext Preprocessor: What is it?
    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP





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