0

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.

0

3 Answers 3

1

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.";
Sign up to request clarification or add additional context in comments.

2 Comments

You're right, I was using $s instead of %s. It works now, thanks. Also, I find it much more clean to use parameters instead of concatenating if I'm dealing with a big string. That's why I wanted to use parameters
@JohnDoe quite contrary, in the long string you will have to count placeholders on your fingers
0

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.";

Comments

0
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.