php printf using %s for digits

I was wondering why not to use printf %s for all kind of variables since it works. I've been using it for so many years. In the manual it says you should use %d for integers and decimals, but why? What is the difference of using %s for integers and decimals against using %d for them? I don't use to give the integer values directly to the printf function but they mostly come from database with type integer and printf works fine. Is it just to be able to give the integers directly to printf function without quotes?

Upvotes: 1

Views: 80

Answers (1)

Ayush Jain
Ayush Jain

Reputation: 349

I think if you want to perform some mathematical operation within printf function, then you would require "%d".

For example,

printf("%d", $x/2); (correct)

printf("%s", $x/2); (incorrect)

Because you won't be able to perform mathematical operations then. Hope it helps!

Upvotes: 1

Related Questions