Programming Basics

  Home arrow Programming Basics arrow Page 5 - 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 - Heredoc


    (Page 5 of 37 )

    This method of enclosing strings can look a little strange to some. It not very commonly used, but provides good functionality. With this technique, you do not need to worry about escaping single or double quotes because it uses neither. For this reason alone, it is appealing to some developers that dislike the chore of escaping quotation marks. We are not going to dwell on this method, but will just provide a simple example and brief explanation.

    <?php
    $mystring 
    = &lt;&lt;&lt;EOD
    Hello World
    !
    This is the second line!
    EOD;
    ?>

    The EOD portion of this syntax is user definable. The important part of this syntax is remembering that the final EOD; must be on a line by itself and must not be indented at all. Everything, up until the closing EOD, will be assigned verbatim to the string. This includes if you forget to close one heredoc and then use another one later on. In this case, everything between the two heredocs will be assigned to the variable from the first. No errors will occur, as it is valid PHP code. Here is an example where we have forgotten to close a heredoc statement.

    <?php
    $mystring 
    = &lt;&lt;&lt;EOD
    Hello World
    !
    This is the second line!
    &
    #160;
    $myotherstring = &lt;&lt;&lt;EOD
    Goodbye
    !
    EOD;
    ?>

    Other than the benefits of not having to escape quotation characters, this syntax also does variable substitution like the double quotes method. As with the double quotes, you must use the complex syntax when dealing with arrays and objects.

    More Programming Basics Articles
    More By Matt Wade

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP

    Developer Shed Affiliates

     



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