Programming Basics

  Home arrow Programming Basics arrow Page 6 - 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 - Concatenation


    (Page 6 of 37 )

    Concatenation gives us an easy way to splice strings together. The syntax is simple and easy to understand. Let's take a look at a simple example of how to combine two strings.

    <?php
    $one 
    'Hello';
    $two 'world';
    $mystring $one $two;
    ?>

    After this code executes, '$mystring' would contain 'Helloworld'. Obviously, it would be preferable to have a space between the two words. In that case, all we need to do is a little more concatenation and we have our desired results.

    <?php
    $one 
    'Hello';
    $two 'world';
    $mystring $one ' ' $two;
    ?>

    Now, the '$mystring' variable would contain 'Hello world'.

    A common use for concatenation is to combine literal strings and variables. Even though we can include the variables inside double quotes, or within the heredoc method, some developers opt to use concatenation instead. It has been argued that using a combination of single quotes and concatenation is faster than the use of double quotes and variables substitution.

    After some testing, we have determined that using single quotes and concatenation is faster. In our tests, we executed each method 100,000 times. This yielded a total difference of one tenth of second for all 100,000 runs on our test machine. You may see a smaller, or larger, difference on your hardware. No matter what, the difference is very small, but in applications where efficiency is number one this could make the difference. For the fun of it, we also tested the heredoc method, and as expected it came in last at almost a tenth of a second behind the double quote method. All this performance testing is fun as an exercise, but it is unlikely that any one quoting method is going to give your application a significant performance boost.

    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 1 - Follow our Sitemap