473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

printf / sprintf function usage

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
5 3452
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
"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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: hello123 | last post by:
Hi, Can i ask the question: If you have an integer(int), how to print it out without using printf? Thanks
3
by: Seong-Kook Shin | last post by:
C FAQ Q 13.2 says that sprintf() is guaranteed to work only for n <= 509: sprintf(dest, "%.*s", n, source); Does 509 appear in any C standard? If not, where it came from? I looked over C99...
2
by: David Mathog | last post by:
Is there some method for restricting the maximum number of characters that will be emitted by a printf() function? Where printf() here is generic and refers to sprintf, fprintf, etc. Here's an...
6
by: pinkfloydhomer | last post by:
I want to have debugging macro or function that I can use like: DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4); just as with printf. I want to avoid implementing this functionality...
16
by: Jordan Abel | last post by:
I have written this function and was wondering if anyone else could point out if there's anything wrong with it. The purpose is to substitute for printf when in a situation where low-level I/O has...
20
by: v4vijayakumar | last post by:
When "printf" function returns "EOF"? Will "printf" ever fail?
6
by: MikeC | last post by:
Folks, Can the width specifier be used in a printf() text string? If I execute... printf("%4s", ".........."); it prints ten dots. If I execute
3
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2",...
43
by: Jrdman | last post by:
someone has an idea on how the printf function is programmed ?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.