473,763 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf / sprintf function usage

Hi,

In awk I can do this:

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

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

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 3468
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.coh p1...
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.coh p1...
>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*******@attgl obal.net
=============== ===

Dec 17 '07 #5

"Jerry Stuckle" <js*******@attg lobal.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.coh p1...
>>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*******@attgl obal.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("Variab le 1: %s Variable 2: %%s", var1);
var2="y";
printf(temp,var 2);
# In awk the above will output: "Variable 1: x Variable 2: y"
*/
$var1="x";
$temp = sprintf("Variab le 1: %s Variable 2: %%s", $var1);
$var2="y";
printf($temp,$v ar2);
// 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
2718
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
14661
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 (ISO/IEC 9899:1999) but didn't find yet. If that limitation is old standard (perhaps ANSI?), does it limit the total length of the string that sprintf() would print, or just for one '%s'?
2
1974
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 example of where this would be used (partial code!) double thevar; char abuf; int oc;
6
6533
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 myself with varargs etc. Now, in my case, I cannot use printf etc to output with. I have to use some other function that can only take a string. Let's call it output(char* str). Today, when debugging, I insert something like:
16
19708
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 to be used for whatever reason, and memory allocation cannot be used - mainly it's become for me an exercise in interpreting printf-like format strings, and i thought i'd post it here to see if anyone can point out any bugs in it - i.e. anything...
20
2351
by: v4vijayakumar | last post by:
When "printf" function returns "EOF"? Will "printf" ever fail?
6
18053
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
2623
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", str); //Interesting stuff happens here printf(str2);
43
366
by: Jrdman | last post by:
someone has an idea on how the printf function is programmed ?
0
9566
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10149
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10003
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8825
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7370
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3918
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2797
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.