/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] sprintf()

I’m trying to extract what I can from the PHP manual at php.net, but I can’t figure it out.

I have a string:

[code=php]
$errspan = “<span class=”error” $s id=”$snotice”> $s </span>”;
[/code]

But when I try to do the following:

[code=php]
$fname = sprintf($errspan,””,”fname”,”You must enter a valid first name”);
[/code]

It gives me the following:

[code=html]
<span class=”error” id=””></span>
[/code]

When what I want is this:

[code=html]
<span class=”error” id=”fnamenotice”>You must enter a valid first name</span>
[/code]

Can anyone help me out with this?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@David_HarrisonNov 02.2008 — PHP is looking for a variable named $snotice, how does it know that you mean $s followed by the word notice?

This is why I prefer to write code more like this:[code=php]$errspan = '<span class="error" '.$s.'id="'.$s.'notice">'.$s.'</span>';[/code]
Copy linkTweet thisAlerts:
@aj_nscauthorNov 02.2008 — Thanks David, that makes more sense. However I am still getting nothing where there should be something, except now the word $notice is there. Is sprintf() the right function that I need here?

What I am getting is this:
[code=html]
<span class="error" id="notice"></span>
[/code]
Copy linkTweet thisAlerts:
@David_HarrisonNov 02.2008 — I think that should be &#37;s not $s in there.[code=php]$errspan = '<span class="error" %sid="%snotice">%s</span>';[/code]
Copy linkTweet thisAlerts:
@Stephen_PhilbinNov 02.2008 — What exactly is it that you're expecting the $s variable to contain?
×

Success!

Help @aj_nsc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...