By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

printf / sprintf function usage

P: n/a
Hi,

In awk I can do this:

var1="x";
temp = sprintf("Variable 1: %s Variable 2: %%s", var1);
# now the value of temp is "Variable 1: x Variable 2: %s"

var2="y";
printf(temp,var2);

The above will output: "Variable 1: x Variable 2: y"

Can I do a similar operation with printf/sprintf in PHP?

Thanks
Dec 17 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Dave wrote:
Can I do a similar operation with printf/sprintf in PHP?
Yes.

RTFM on sprintf and printf: http://php.net/printf ; http://php.net/sprintf

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
Dec 17 '07 #2

P: n/a
"Iván Sánchez Ortega" <ivansanchez-alg@rroba-escomposlinux.-.punto.-.org>
wrote in message news:fk**********@hercules.cohp1...
Dave wrote:
>Can I do a similar operation with printf/sprintf in PHP?

Yes.

RTFM on sprintf and printf: http://php.net/printf ; http://php.net/sprintf
I did, read the manual before posting. There is nothing mentioning the usage
I have described in my original post. Note question was, whether I could use
%%s in the first instance of sprintf and than use the resulting %s literal
as a placeholder to the second printf.
Dec 17 '07 #3

P: n/a
Dave wrote:
Note question was, whether I could use %%s in the first instance of
sprintf and than use the resulting %s literal as a placeholder to the
second printf.
Yes.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Q: What's the difference between an Irish wedding and an Irish wake?
A: One less drunk.

Dec 17 '07 #4

P: n/a
Dave wrote:
"Iván Sánchez Ortega" <ivansanchez-alg@rroba-escomposlinux.-.punto.-.org>
wrote in message news:fk**********@hercules.cohp1...
>Dave wrote:
>>Can I do a similar operation with printf/sprintf in PHP?
Yes.

RTFM on sprintf and printf: http://php.net/printf ; http://php.net/sprintf

I did, read the manual before posting. There is nothing mentioning the usage
I have described in my original post. Note question was, whether I could use
%%s in the first instance of sprintf and than use the resulting %s literal
as a placeholder to the second printf.
Dave,

No, it didn't say that in so many words - but the rules sprintf() and
printf() follow are in there.

And yes, it works just fine. Did you try it?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Dec 17 '07 #5

P: n/a

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:G9******************************@comcast.com. ..
Dave wrote:
>"Iván Sánchez Ortega" <ivansanchez-alg@rroba-escomposlinux.-.punto.-.org>
wrote in message news:fk**********@hercules.cohp1...
>>Dave wrote:

Can I do a similar operation with printf/sprintf in PHP?
Yes.

RTFM on sprintf and printf: http://php.net/printf ;
http://php.net/sprintf

I did, read the manual before posting. There is nothing mentioning the
usage I have described in my original post. Note question was, whether I
could use %%s in the first instance of sprintf and than use the resulting
%s literal as a placeholder to the second printf.

Dave,

No, it didn't say that in so many words - but the rules sprintf() and
printf() follow are in there.

And yes, it works just fine. Did you try it?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jerry,

Yes, I tried the concept. For some reason it did not work in the real code I
was using, but the awk example I provided in my original post worked when I
rewrote for PHP. So I now know it's working. Thanks:
<?php
/*
# awk example
var1="x";
temp = sprintf("Variable 1: %s Variable 2: %%s", var1);
var2="y";
printf(temp,var2);
# In awk the above will output: "Variable 1: x Variable 2: y"
*/
$var1="x";
$temp = sprintf("Variable 1: %s Variable 2: %%s", $var1);
$var2="y";
printf($temp,$var2);
// In PHP the above will output: "Variable 1: x Variable 2: y"
?>
Dec 18 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.