473,769 Members | 4,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Newline (\n) in printf doesn't work?

I'm new to PHP.

I see that PHP supports the C printf function, and I've seen examples
like printf("Hello world!\n"); however the newline character \n
doesn't work - i.e., it does not generate an HTML <br>, which I would
have expected - what it does is generate a newline in the html
generated text, but since the browser ignores blank lines, this
feature appears useless for most applications.
>From this arises two questions:
1. I am misunderstandin g something?

2. What is the prescribed way of inserting a newline into a PHP
string? (Could it be printf("Hello world! <br>") ?

Thanks,
M. McDonnell

May 28 '07 #1
11 24923
Michael wrote:
I'm new to PHP.

I see that PHP supports the C printf function, and I've seen examples
like printf("Hello world!\n"); however the newline character \n
doesn't work - i.e., it does not generate an HTML <br>, which I would
have expected - what it does is generate a newline in the html
generated text, but since the browser ignores blank lines, this
feature appears useless for most applications.
>>From this arises two questions:

1. I am misunderstandin g something?

2. What is the prescribed way of inserting a newline into a PHP
string? (Could it be printf("Hello world! <br>") ?

Thanks,
M. McDonnell
It works fine. It generates a nl character, just as you asked it to.
Look at your source code - you'll see the line break.

If you want an HTML <bror xthtml <br />, you need to code them
yourself. PHP does not convert for you.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
May 28 '07 #2
On May 27, 8:05 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
Michael wrote:
I'm new to PHP.
I see that PHP supports the C printf function, and I've seen examples
like printf("Hello world!\n"); however the newline character \n
doesn't work - i.e., it does not generate an HTML <br>, which I would
have expected - what it does is generate a newline in the html
generated text, but since the browser ignores blank lines, this
feature appears useless for most applications.
>From this arises two questions:
1. I am misunderstandin g something?
2. What is the prescribed way of inserting a newline into a PHP
string? (Could it be printf("Hello world! <br>") ?
Thanks,
M. McDonnell

It works fine. It generates a nl character, just as you asked it to.
Look at your source code - you'll see the line break.

If you want an HTML <bror xthtml <br />, you need to code them
yourself. PHP does not convert for you.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attgl obal.net
=============== ===- Hide quoted text -

- Show quoted text -
Jerry,

Thanks for your reply. Yes, I looked at the source code, but no <br>,
all I get is a new line of white space, which as we know is ignored by
the browser.

May 28 '07 #3
rf

"Michael" <Mi************ ***@yahoo.comwr ote in message
news:11******** **************@ j4g2000prf.goog legroups.com...
I'm new to PHP.

I see that PHP supports the C printf function, and I've seen examples
like printf("Hello world!\n"); however the newline character \n
doesn't work - i.e., it does not generate an HTML <br>, which I would
have expected - what it does is generate a newline in the html
generated text, but since the browser ignores blank lines, this
feature appears useless for most applications.
>>From this arises two questions:

1. I am misunderstandin g something?

2. What is the prescribed way of inserting a newline into a PHP
string? (Could it be printf("Hello world! <br>") ?
Yes.

More properly

printf("Hello world! <br>\r\n");

so you get a line break in the source HTML as well, making it easier to
read.

or, since you are not actually formatting anything:

print("Hello world! <br>\r\n");

or even

echo "Hello world! <br>\r\n";

--
Richard.
May 28 '07 #4
Michael wrote:
On May 27, 8:05 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
>Michael wrote:
I'm new to PHP.
I see that PHP supports the C printf function, and I've seen
examples like printf("Hello world!\n"); however the newline
character \n doesn't work - i.e., it does not generate an HTML
<br>, which I would have expected - what it does is generate a
newline in the html generated text, but since the browser ignores
blank lines, this feature appears useless for most applications.
>>From this arises two questions:
1. I am misunderstandin g something?
2. What is the prescribed way of inserting a newline into a PHP
string? (Could it be printf("Hello world! <br>") ?

It works fine. It generates a nl character, just as you asked it to.
Look at your source code - you'll see the butline break.

If you want an HTML <bror xthtml <br />, you need to code them
yourself. PHP does not convert for you.

Thanks for your reply. Yes, I looked at the source code, but no <br>,
all I get is a new line of white space, which as we know is ignored by
the browser.
That's correct. You didn't ask PHP to write a <brtag. You asked PHP to
write a newline break, which is exactly what it did.

So yes, your question 2) is what you would need to do, although your
question should read like this:

2. What is the prescribed way of inserting an *html* *newline* break*
*tag* into a PHP string? (Could it be printf("Hello world! <br>") ?

There is also the nl2br() function which converts "\n" to "<br />\n"
should you need to automatically need to do this with a string.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
May 28 '07 #5
On May 27, 8:20 pm, Chris Hope <blackh...@elec trictoolbox.com wrote:
Michael wrote:
On May 27, 8:05 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
Michael wrote:
I'm new to PHP.
I see that PHP supports the C printf function, and I've seen
examples like printf("Hello world!\n"); however the newline
character \n doesn't work - i.e., it does not generate an HTML
<br>, which I would have expected - what it does is generate a
newline in the html generated text, but since the browser ignores
blank lines, this feature appears useless for most applications.
>From this arises two questions:
1. I am misunderstandin g something?
2. What is the prescribed way of inserting a newline into a PHP
string? (Could it be printf("Hello world! <br>") ?
It works fine. It generates a nl character, just as you asked it to.
Look at your source code - you'll see the butline break.
If you want an HTML <bror xthtml <br />, you need to code them
yourself. PHP does not convert for you.
Thanks for your reply. Yes, I looked at the source code, but no <br>,
all I get is a new line of white space, which as we know is ignored by
the browser.

That's correct. You didn't ask PHP to write a <brtag. You asked PHP to
write a newline break, which is exactly what it did.

So yes, your question 2) is what you would need to do, although your
question should read like this:

2. What is the prescribed way of inserting an *html* *newline* break*
*tag* into a PHP string? (Could it be printf("Hello world! <br>") ?

There is also the nl2br() function which converts "\n" to "<br />\n"
should you need to automatically need to do this with a string.

--
Chris Hope |www.electricto olbox.com|www.linuxcdmall.com- Hide quoted text -

- Show quoted text -
Thank you all for your help. I now understand all of this a bit
better.

May 28 '07 #6
Michael <Mi************ ***@yahoo.comwr ote:
>
I'm new to PHP.

I see that PHP supports the C printf function, and I've seen examples
like printf("Hello world!\n"); however the newline character \n
doesn't work - i.e., it does not generate an HTML <br>, which I would
have expected - what it does is generate a newline in the html
generated text, but since the browser ignores blank lines, this
feature appears useless for most applications.
What about <preor <textarearegion s? What if I care about the way my
generated HTML looks, and I want things to line up neatly?
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
May 30 '07 #7
Tim Roberts wrote:
Michael <Mi************ ***@yahoo.comwr ote:
>I'm new to PHP.

I see that PHP supports the C printf function, and I've seen examples
like printf("Hello world!\n"); however the newline character \n
doesn't work - i.e., it does not generate an HTML <br>, which I would
have expected - what it does is generate a newline in the html
generated text, but since the browser ignores blank lines, this
feature appears useless for most applications.

What about <preor <textarearegion s? What if I care about the way my
generated HTML looks, and I want things to line up neatly?
The first rule of HTML is - make your layout fluid! Allow it to adjust
to the size of the user's browser window.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
May 30 '07 #8
Jerry Stuckle <js*******@attg lobal.netwrote:
>Tim Roberts wrote:
>Michael <Mi************ ***@yahoo.comwr ote:
>>I'm new to PHP.

I see that PHP supports the C printf function, and I've seen examples
like printf("Hello world!\n"); however the newline character \n
doesn't work - i.e., it does not generate an HTML <br>, which I would
have expected - what it does is generate a newline in the html
generated text, but since the browser ignores blank lines, this
feature appears useless for most applications.

What about <preor <textarearegion s? What if I care about the way my
generated HTML looks, and I want things to line up neatly?

The first rule of HTML is - make your layout fluid! Allow it to adjust
to the size of the user's browser window.
What I meant is, "what if I want my HTML code to line up neatly?" For
example:
<select name="City">
<option>Alban y</option>
<option>Portlan d</option>
<option>Salem </option>
<option>Tigar d</option>
</select>

The browser would be perfectly happy with one immensely long line, of
course, but it's not unreasonable for me to want the HTML to look like the
above, and that requires printing \n newlines.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 1 '07 #9
Tim Roberts wrote:
Jerry Stuckle <js*******@attg lobal.netwrote:
>Tim Roberts wrote:
>>Michael <Mi************ ***@yahoo.comwr ote:
I'm new to PHP.

I see that PHP supports the C printf function, and I've seen examples
like printf("Hello world!\n"); however the newline character \n
doesn't work - i.e., it does not generate an HTML <br>, which I would
have expected - what it does is generate a newline in the html
generated text, but since the browser ignores blank lines, this
feature appears useless for most applications.
What about <preor <textarearegion s? What if I care about the way my
generated HTML looks, and I want things to line up neatly?
The first rule of HTML is - make your layout fluid! Allow it to adjust
to the size of the user's browser window.

What I meant is, "what if I want my HTML code to line up neatly?" For
example:
<select name="City">
<option>Alban y</option>
<option>Portlan d</option>
<option>Salem </option>
<option>Tigar d</option>
</select>

The browser would be perfectly happy with one immensely long line, of
course, but it's not unreasonable for me to want the HTML to look like the
above, and that requires printing \n newlines.
What does this have to do with converting newline characters to <br>?
Newline characters will affect the generated html, <brwon't.

But if you really want to bloat your html with lots of extra white
space, increasing the amount of bandwidth required to load your pages
and slow down your customers, by all means, do it.

Now I'm not suggesting you have no formatting - but you can overdo it, also.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Jun 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
9820
by: Michael Bradley-Robbins | last post by:
I am writing a program, and I am using \n in double-quoted print statements, which should give me a line break. It doesn't work. I'm running Apache 2.0 and PHP 5, with Mozilla Firefox as a browser. What is wrong?
7
4892
by: AnnMarie | last post by:
My JavaScript Form Validation doesn't work at all in Netscape, but it works fine in IE. I made some of the suggested changes which enabled it to work in IE. I couldn't make all the changes because then it didn't work in IE. How can I enable this javascipt form validation to work in Netscape? When I use netscape, none of the alert boxes appear. It submits the form without validating anything.
11
7500
by: C. Sengstock | last post by:
Hi, i want to save the keywords of an ini file in a struct, together with a fpos_t. I think i´m right with the concept, but the access through fsetpos() doesn´t work. The position is always wrong (except, in this example, the first line). My struct looks like this: *** typedef struct { char pname;
9
2638
by: Erik Leunissen | last post by:
L.S. I've observed unexpected behaviour regarding the usage of the '#' flag in the conversion specification in the printf() family of functions. Did I detect a bug, or is there something wrong with my expectations regarding the effect of the following code: printf("NULL as hex: %#4.2x\n", '\0');
6
13302
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileID.ToString() + ".pdf");
4
3408
by: bbp | last post by:
Hello, In an ASPX page I have a "Quit" button which make a simple redirect in code-behind. This button doesn't work no more since (I think) I moved from the framework 1.0 to 1.1 and it doesn't work only on IE! My ASPX page works in FireFox. If the button doesn't work it's because of validators (and it worked with the same code in v1.0).
185
17467
by: Martin Jørgensen | last post by:
Hi, Consider: ------------ char stringinput ..bla. bla. bla. do {
13
1817
by: Scorpio | last post by:
hi, I'm trying to run a bubble-sort. But the while() in main doesn't work. Can somebody tell me why? thanks a lot~~~ here is the code #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 20
10
1840
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET framework 2.0, and he had 1.0 and it didn't work; then he installed 2.0 and it still didn't work; so he tried with 2.1 and it didn't work, then 3.0 and nothing still worked. I have Intel Centrino Mobile (laptop computer), and he has Intel Pentium...
0
9589
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
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
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
9994
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
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
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
7408
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...
1
3958
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
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.