[SOLVED] What is sprintf

This is a discussion on "[SOLVED] What is sprintf" within the PHP Forum section. This forum, and the thread "[SOLVED] What is sprintf are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 15th, 2007, 21:48
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
[SOLVED] What is sprintf

I have been trying to teach myself OOP in php and it looks pretty straightforward but the small examples look a bit longwinded when you could do it simpler but I can see it's uses.

Anyway, one example i was reading through used the sprintf() function.

I looked at what it does but i don't really understand it. I went to the w3schools website and found this function
PHP: Select all

<?php
$str 
"Hello";
$number 123;
$txt sprintf("%s world. Day number %u",$str,$number);
echo 
$txt;
?>
Now the output i was expecting was "Hello world. Day number Hello123"

This is becuase the variable $str is before $number. Why doesn't it do as i expect?
Reply With Quote

  #2 (permalink)  
Old Oct 16th, 2007, 06:20
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
Re: What is sprintf

Try this:
PHP: Select all

   <?php
$format 
'The %2$s contains %1$d monkeys';
echo(
sprintf($format$num$location));
// OR
printf($format$num$location);
?>
And check out the documentation here.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax of sprintf sudhakararaog PHP Forum 4 May 22nd, 2008 14:06
[SOLVED] 1 solved problem causes another delusion Web Page Design 9 Dec 21st, 2007 08:12
Adding "Topic Solved" for solved topics AdRock Webforumz Suggestions and Feedback 21 Oct 4th, 2007 15:08


All times are GMT. The time now is 02:15.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43