Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using 'printf' to return string, not print it

Tags:

string

php

printf

This may sound strange, but here goes.

I like using this technique of building a string in php

printf(__('This is %1$s, this is %2$s'), myFunction1(), myFunction2());

Obviously this directly prints the results whenever the function is called, but I would like to use this technique to just build a string, and then use it later elsewhere.

Is this possible?

Thanks guys.

like image 340
shane Avatar asked Sep 29 '11 16:09

shane


People also ask

Which of the following printf statement will not print a string?

printf() is not printing any thing because the string mentioned is empty and in if it will also return the number of character printed which is 0. So it will proceed in else block instead of if. Here a[] is empty and in if statement we are printing a[], hence nothing will be printed.

Why is it printf and not print?

The most basic printing functions would be puts and putchar which print a string and char respectively. f is for formatted. printf (unlike puts or putchar ) prints formatted output, hence printf.

Does printf print a string?

C++ printf is a formatting function that is used to print a string to stdout.

Why printf in C is not working?

Printf is not the thing being buffered, it's stdio, so all similar functions will behave in the same way. To demonstrate the buffering, all you have to do is printf a lot of characters, usually more than 1024, and printf will print as you will have exceeded the maximum buffer length and it will automatically fflush.


1 Answers

Use sprintf to do this:

$var = sprintf(__('This is %1$s, this is %2$s'), myFunction1(), myFunction2());
like image 53
Jason Avatar answered Oct 02 '22 08:10

Jason