federicot
federicot

Reputation: 12341

Using printf with parameters

How can I use printf to work with parameters regardless of their type? (Something like binding parameters in a prepared statement in PDO).

E.g.:

printf("Hello $s, you have $s new notifications.", $username, $newNotifications);

I tried something like that and didn't work.

Upvotes: 0

Views: 104

Answers (3)

Your Common Sense
Your Common Sense

Reputation: 157870

echo "Hello $username, you have $newNotifications new notifications.";

note that good syntax highlighter will emphase variables in the string for the even better readability, making this way simply the best.

Upvotes: 0

staticsan
staticsan

Reputation: 30555

Look at the man page.

Basically, you're using $s when you should be using %s. But you could just use print or echo with the string:

print "Hello ".$username.", you have ".$newNotifications." new notifications.";

Upvotes: 1

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137398

printf needs to know how to format the arguments. You could just echo them instead, using the . concatenation operator:

 echo "Hello " . $username . ", you have " . $newNotifications . " new notifications.";

Upvotes: 0

Related Questions