Xhynk
Xhynk

Reputation: 13840

Changing printf() to print() safely?

So I for some reason or another adopted using printf() as a replacement for echo or print

I don't have any formatted strings that REQUIRE printf($var);, so they could (and should) all be turned into print($var);

Since they've all got the same syntax printf($this.'that'); could I just do a folder-wide "find & replace" to change printf( to print( safely?

Upvotes: 0

Views: 191

Answers (2)

jimp
jimp

Reputation: 17487

If you have calls to printf that do not expect formatting, you are going to run into problems and they really should be changed to print. For example, if your input to printf contains the % character, you will get an error instead about having a badly formatted argument string.

$something = 'Results: 10% complete.';
print($something);
printf($something); // ERROR: printf() thinks it should replace "% c"
                    //        with an integer not passed as a second argument
printf('%s',$something); // Pass unquoted input as a formatted argument

So basically, if your code has never used printf with consciously formatted input, you would benefit from a find/replace in your code to convert all those calls to print.

Upvotes: 1

Green Black
Green Black

Reputation: 5084

If you are sure you don't use printf functionality anywhere, and print is sufficient, than yes, you can replace it all with a simple search/replace.

Upvotes: 0

Related Questions