Programming Basics
  Home arrow Programming Basics arrow Page 10 - PHP Strings Primer
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PROGRAMMING BASICS

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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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


       · comment
       · really goood work ,it covers all the major string functions.also explained with...
       · test'ng
     

    PROGRAMMING BASICS ARTICLES

    - PHP: Hypertext Preprocessor: What is it?
    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT