Programming Basics

  Home arrow Programming Basics arrow Page 9 - 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 - print


    (Page 9 of 37 )

    Like echo, print is a language construct of PHP, not a function. The use of print is almost identical to echo. There are, however, some subtle differences.

    First of all, print returns 'TRUE' if it was able to successfully output the string passed to it or 'FALSE' if it was not. The fact that print' returns a value allows it to be used in situations where echo can not be used. Take an expression like the following:

    <?php
    $var 
    ? print 'true' : print 'false';
    ?>

    In this example, we are using the ternary operator to decide what action to take. The ternary operator is a very simple if-else statement. It has three different parts. The first is the evaluation expression, which in our case is simply $var. The next expression, print 'true', is evaluated only if the first expression is TRUE. The last expression, print 'false', is evaluated if the first is FALSE. It can be read as, 'If $var is true print true, otherwise, print false.

    If we were to replace the 'print' statements with 'echo' statements, it would result in a parse error. Because echo does not return a value it would not result in a valid expression, whereas print would.

    Another difference between print and echo is the later is able to take multiple arguments. As we saw when we examined echo, you can pass multiple strings to it separated by commas. The print construct, on the other hand, will only accept one argument or string.

    Finally, it has been argued that print is not quite as fast as echo is. While this may be true, and verified by our tests, in order to see any noticeable difference we would need to use hundreds of thousands of print statements in a single script. Even then, the difference would only be very slight. if your application relies on efficiency and speed of execution, you may be better off using echo. For most applications, however, the difference will not be seen.

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