Programming Basics

  Home arrow Programming Basics arrow Page 10 - 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 - printf


    (Page 10 of 37 )

    The last two techniques for displaying data that we discussed were very similar in nature. The 'printf' function, on the other hand, is very different from either echo or print though it may seem comparable on the surface. The first difference is that it is indeed a function, unlike the first two techniques which were language constructs.

    In its very basic use, 'printf' does work just as the first two display methods we looked at. Take for instance this example:

    <?php
    printf
    ("Hello");
    ?>

    With this particular example, we could exchange 'echo' or 'print' for the 'printf()' statement and it would essentially work just the same. Do note however that 'printf()' requires parentheses unlike either of the other two techniques due to the fact that it is a function and not a language construct.

    The major differences with 'printf()' come when we want to provide formatted output of variables. As we saw earlier, with 'echo' and 'print' we can just include the variable name within a double quoted string and it will be outputted, although we have no control over its format. We can do the same with 'printf()', but we can also take it a step further. The 'printf()' function allows us to put placeholders in our output string and then pass the variables in as additional arguments. We will see in a moment how this allows us to tap into the power of printf()', but first let's just look at a simple example.

    <?php
    $var 
    'Hello';
    printf ("She said '%s' to me."$var);
    ?>

    This will output:

    She said 'Hello' to me.

    The value we passed has been placed into the string at the point we put the placeholder. In this case we used a placeholder of '%s'. This signifies that we want to output a string. Other place holders include '%d' for integers and '%f' for floating point numbers. In order to include a literal percent symbol, we must use a double percent symbol, '%%'.

    The real power of 'printf()', however, is in its ability to format the string before outputting it. A common use of the formatting abilities of printf is to pad a number for display. This allows you to be sure that your output is consistent. In the following example, we will output a number and pad it with zeros to ensure that it displays as five characters.

    <?php
    $var 
    "12";
    printf ("A padded number: %05d"$var);
    ?>

    This piece of code will output the following:

    A padded number: 00012

    As you can see, the number twelve is displayed with three zeros preceding it. If the number had been single digit, rather than double digit, four zeros would have preceded it. Using this method to pad numbers, we can produce consistent and easy to read output.

    Another formatting capability of 'printf()' is to specify the amount of significant digits displayed for floating point numbers. Again, this can be useful for readability and maintaining conformity within output. Let's look at another bit of code that uses this functionality.

    <?php
    $var 
    1.56823;
    printf ("This number has 2 significant digits: %.2f"$var);
    ?>

    This will output:

    This number has 2 significant digits: 1.57

    Notice how it only displays two significant digits? Also, pay special attention to the fact that 'printf()' rounded the number up for us, rather than just dropping the extra digits.

    The 'printf()' function is not nearly as efficient, time-wise, as echo or print, but the benefits gained from formatted output can make choosing this function simple if the functionality is needed. Here we have just taken a quick tour of the 'printf()' functions capabilities, we implore you to take a closer look into its functionality.

    Another function that works almost identically to 'printf()' is the 'sprintf()' function. The only difference between the two is that 'sprintf()' will return the string, so that you may store it in a variable or use it in another function, rather than display it as 'printf()' does.

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