Programming Basics
  Home arrow Programming Basics arrow Page 4 - 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 - Double Quotes


    (Page 4 of 37 )

    Using double quotes gives us the ability to parse variables within our strings. To demonstrate this, let's look at a simple illustration:

    <?php
    $var 
    'Hello';
    $mystring "$var world";
    ?>

    After these two lines of code execute, '$mystring' will contain the string 'Hello world'. So, as you can see, within double quotes variables are seen as variables and variable substitution occurs. This simple variable parsing doesn't work in all situations however. If, for instance, you wanted to use a value stored in an array rather than just the simple variable we have above, you would need to use what is called complex syntax. Complex syntax allows you to use objects and arrays in strings. A syntax error will occur if an object, or an array that utilizes an associative index, is included in a string without using complex syntax. To avoid the syntax error, we should make use of complex syntax by encapsulating the object or array in curly braces. The following example will demonstrate this syntax.

    <?php
    $mystring 
    "This is an array value: {$myarray['foo']}.";
    ?>

    As with single quotes, we can place our string on multiple lines to place newlines in them. But, we also have another method available to us. By using the special sequence of '\n' we can also place a newline in a string.

    <?php
    $mystring 
    "This is the first line,\n and this is the second line";
    ?>

    We can also place horizontal tabs in our strings by using '\t'. If we want to include a double quote in the string, we can use the same method we explored with single quotes of escaping the double quote with a backslash character. In both the single and double quote methods, we should also escape the backslash character if we want it displayed. Let's take a look at an example with these other escape sequences.

    <?php
    $mystring 
    "\tThis should be tabbed over\nAnd here is a newline.
    Now, let's display a backslash \\."
    ;
    ?>

    Below is a list which details each escape sequence.

  • \n -linefeed or newline (LF or 0x0A (10) in ASCII)
  • \r -carriage return (CR or 0x0D (13) in ASCII)
  • \t -horizontal tab (HT or 0x09 (9) in ASCII)
  • \\ -backslash
  • \$ -dollar sign
  • \" -double-quote
  • \[0-7]{1,3} -the sequence of characters matching the regular expression is a character in octal notation
  • \x[0-9A-Fa-f]{1,2} -the sequence of characters matching the regular expression is a character in hexadecimal notation

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek