SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've noticed that a lot of PHP coders frequently use these functions to place variables in a string - for example:

    pprintf("<td>Date: %s</td><td>%s</td><tr>", $date, $title);

    My question: Why use this when the following (in my opinion at least) is a lot more readable:

    print "<td>Date: $date</td><td>$title</td><tr>";

    I ask mainly because Professional PHP (Wrox Press) has entire chapters with lengthy example scripts that use this EVERYWHERE - and in my mind it only makes things look a lot mroe complicated than they actually are. Is there any performance or code style reason to use these functions?

    Cheers,

    Skunk

  2. #2
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I should add I understand using them for formatting numbers (decimal points, currency stuff etc) but I just don't understand why they are used for whole strings like the example above.

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually form everything I have read printf() and sprintf() are more system intensive than print but not enough to make that big a difference. Normally printf() and sprintf() are used when one needs to format a string lets say we want to make numbers always have four digits and any extra needed will be 0's before the number of them sure you could use

    PHP Code:
    for($i=1;$i<3;$i++) {
    $newnum .= "0".$newnum
    }
    $newnum $newnum.$num
    or you could use
    PHP Code:
    $num sprintf('%04d'$num); 
    printf() is the same as sprintf() except for it prints the formatted string to the screen instead saving it to a variable for later use as sprintf() does.

    On to your examples, when you have this
    PHP Code:
    print "<td>Date: $date</td><td>$title</td><tr>"
    What if you have <td width="100"> Now you can't use the double quotes without escaping them by putting a \ behind them. In my opinion this is sloppy, you can't use single quotes to encapsulate the whole thing because you have varibales in it. YOu could do this


    PHP Code:
    print '<td width="100">Date: '.$date.'</td><td>'.$title.'</td><tr>'
    But again in my opinion this is sloppier I prefer to have the vars listed at the end it is easier for me to read. Like:
    PHP Code:
    printf('<td width="100">Date: %s</td><td>%s</td><tr>'$date$title); 
    Here you can use single quotes and double quotes inside the statement without having to escape them. It really boils down to personal choice though, and I hope I have provided you with a different look at it that might help you to see the benefits and drawbacks of both methods.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oooh - I like the single quotes thing. Still won't be using them very often though, I'm too used to lovely messy double quoted strings...

    To be honest I usually leave out quotes around values like that completely, and only both with them when the variable within the quotes might have a space in it.

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah it really is a personal choice, but keep in mind, if the whole xhtml thing takes off you will needthe double quotes around your html tag attributes.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bah - XHTML = no more lazy HTMl coding

    I'm sitting on the fence with XHTML until it either starts being widespread or I think I can make lots of money out of it

  7. #7
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    To add to this question could i do this:

    PHP Code:
    <?
    printf
    ('<td width="100">Date: %s</td><td>%s</td><td>%s</td><tr>'$date$title$name);
    ?>
    Thanx

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course, did you try it, you can specify as many arguments or variables to a statement as long as the same number of placeholders exist.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I understand properly what was going on when you did it in that part of the script for me. Thanx allot.

    Peter


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •