Programming Basics

  Home arrow Programming Basics arrow Page 4 - PHP Strings Primer
PROGRAMMING BASICS

PHP Strings Primer
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus
  • PROGRAMMING BASICS ARTICLES

    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP
    - Calendar Construction with PHP
    - PHP`s Calendar Package
    - Getting Modified Versions and Correct Dates ...
    - Combining Date Functions in PHP
    - Using PHP for Date and Time in Programming
    - More Exception Handling with PHP
    - Exception Handling in PHP
    - Error Logging and Handling Exceptions
    - Configuration Directives for Error and Excep...
    - Error and Exception Handling
    - Python Modules for Games


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap