473,401 Members | 2,127 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,401 software developers and data experts.

Newline

Hello Everyone,

I am running PHP for Apache on a Windows XP machine.

When I attempt to use \n in my code to generate a newline, it does not
work. I've also see the other posts about using \r along with \n, but
that doesn't work either. I'm sort of stuck here. Is there anything
that I can do to generate a newline?

Apr 13 '06 #1
16 2126
infernon wrote:
When I attempt to use \n in my code to generate a newline, it does not
work. I've also see the other posts about using \r along with \n, but
that doesn't work either. I'm sort of stuck here. Is there anything
that I can do to generate a newline?


My bet is that you are doing this:

echo 'Test\n';

The problem here is the single quotes. The single quotes tell the parser to
just spit out this string, and ignore any modifiers or variables.

So, try this:

echo 'Test'."\n";
Good luck,
Carl

--
Carl Vondrick
Web-Engineer
www.carlsoft.net
Apr 13 '06 #2
Thanks for the quick response, Carl.

I'm actually using double-quotes. The exact code is:
printf("%1d %9s at \$%.2f each: \$%.2f\r\n"...
What I've found is that the source actually contains a carriage return,
but that the page doesn't render with one. I've tried this with both
IE and FF to be sure that it wasn't a browser problem. The other side
of it is that the command line puts the carriage return in as well.

I apologize, I should have posted more information in my initial post.
Thanks for your response.

Apr 13 '06 #3
infernon wrote:
What I've found is that the source actually contains a carriage return,
but that the page doesn't render with one. I've tried this with both
IE and FF to be sure that it wasn't a browser problem. The other side
of it is that the command line puts the carriage return in as well.


Ah! Your problem is with the HTML output, then, rather than PHP. HTML
ignores white space. If you want to put a line return, you must put a
break in: <br /> or <br> depending on the markup you are using (XHTML OR
HTML).

Alternatively, you can wrap your code in <pre> tags, which will print
everything exactly as it is.

Carl

--
Carl Vondrick
Web-Engineer
www.carlsoft.net
Apr 13 '06 #4
infernon wrote:
Hello Everyone,

I am running PHP for Apache on a Windows XP machine.

When I attempt to use \n in my code to generate a newline, it does not
work. I've also see the other posts about using \r along with \n, but
that doesn't work either. I'm sort of stuck here. Is there anything
that I can do to generate a newline?


Look at your source - you'll see the newlines in there. But HTML ignores
newline characters. You need to use an html <br>.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 13 '06 #5
OK, so I put the following line in and it worked:

print "</br>";

Is this correct practice?

Apr 13 '06 #6
infernon wrote:
print "</br>";

Is this correct practice?

No, it needs to be either <br> or <br /> depending on what markup language
you are using (HTML uses <br> and XHTML uses <br />).

On the PHP side, the following is a little bit faster:

echo '<br />';

--
Carl Vondrick
Web-Engineer
www.carlsoft.net
Apr 13 '06 #7
Thank you, Carl. This was a big help!

Apr 13 '06 #8
infernon wrote:
OK, so I put the following line in and it worked:

print "</br>";

Is this correct practice?


Yep. Or, if your source is a string containing nl characters, use nl2br().

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 13 '06 #9
Carl Vondrick wrote:
infernon wrote:
print "</br>";

Is this correct practice?


No, it needs to be either <br> or <br /> depending on what markup language
you are using (HTML uses <br> and XHTML uses <br />).

On the PHP side, the following is a little bit faster:

echo '<br />';


But <br /> is invalid html 4.x, although browsers will generally process it OK.
It's only valid for xml or xhtml.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 13 '06 #10
Jerry Stuckle wrote:
But <br /> is invalid html 4.x, although browsers will generally process
it OK. It's only valid for xml or xhtml.

Yes, as I said above. :-)

--
Carl Vondrick
Web-Engineer
www.carlsoft.net
Apr 13 '06 #11
On Thu, 13 Apr 2006 17:39:50 +0000, Carl Vondrick wrote:
print "</br>";

On the PHP side, the following is a little bit faster:

echo '<br />';


While I don't dispute using echo instead of print and ' instead of " is
faster, has anyone actually done any benchmarks to see how much faster.

To be honest, I tend to use print (because I'm used to it) and "" (as I
generally would go back and add variables in later and may forget to
change the ' to ").

It's the same reason that in various coding standards it's recommended to
use {} even for single line ifs, so when the coder returns (or another one
edits the file) they're less likely to add a second indented line and not
see that the statement isn't wrapped in braces.

It's also faster to write functions inline, but at the expense of
readability/ease of maintenance. Not that we're comparing apples to
apples with that latter analogy (I know there's not much readability
difference in echo '' and print "").

Anyway, my point was - undeniably faster, but how much so... Anyone got
numbers?

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Apr 13 '06 #12
Jerry Stuckle wrote:
infernon wrote:

When I attempt to use \n in my code to generate a newline, it does
not work. I've also see the other posts about using \r along with
\n, but that doesn't work either. I'm sort of stuck here. Is
there anything that I can do to generate a newline?


Look at your source - you'll see the newlines in there. But HTML
ignores newline characters. You need to use an html <br>.


It doesn't ignore it, rather treats it as whitespace.

Brian
Apr 13 '06 #13
Andy Jeffries wrote:
Anyway, my point was - undeniably faster, but how much so... Â*Anyone got
numbers?

In large numbers, it is significant. Ran under Kubuntu Linux with PHP 5.1.2
with 1GB RAM and 3.2Ghz processor:

Output:
S Diff: 11.428... (single quotes)
D Diff: 19.623... (double quotes)

Script:
<?php
$s_start = microtime(true);

for ($x = 0; $x < 99999; $x++)
{
echo 'Hello. How are you?<br />';
}

$s_end = microtime(true);

$d_start = microtime(true);

for ($x = 0; $x < 99999; $x++)
{
echo "Hello. How are you?<br />";
}

$d_end = microtime(true);

echo 'S Diff: ' . ($s_end - $s_start) . '<br />';
echo 'D Diff: ' . ($d_end - $d_start) . '<br />';
?>
So, single quotes wins hands down. To compare echo vs print:

Output:
E Diff: 12.626... (echo)
P Diff: 15.637... (print)

<?php
$e_start = microtime(true);

for ($x = 0; $x < 99999; $x++)
{
echo 'Hello. How are you?<br />';
}

$e_end = microtime(true);

$p_start = microtime(true);

for ($x = 0; $x < 99999; $x++)
{
print 'Hello. How are you?<br />';
}

$p_end = microtime(true);

echo 'E Diff: ' . ($e_end - $e_start) . '<br />';
echo 'P Diff: ' . ($p_end - $p_start) . '<br />';
?>

The fastest is echo with single quotes.

--
Carl Vondrick
Web-Engineer
www.carlsoft.net
Apr 13 '06 #14
On Thu, 13 Apr 2006 10:22:12 -0700, infernon wrote:
Is this correct practice?


print "<br>";

--
http://www.mgogala.com

Apr 14 '06 #15
xhtml - <br />

Apr 14 '06 #16
On Thu, 13 Apr 2006 21:30:57 +0000, Carl Vondrick wrote:
Anyway, my point was - undeniably faster, but how much so... *Anyone
got numbers? In large numbers, it is significant. Ran under Kubuntu Linux with PHP
5.1.2 with 1GB RAM and 3.2Ghz processor:

Output:
S Diff: 11.428... (single quotes)
D Diff: 19.623... (double quotes)


OK, something's awry!

I'm running on a 4800+ X2 (although running a script like this will
effectively only use a single 2.4GHz processor with a 1MB cache) using
PHP 5.1.2 on Gentoo (desktop) and I get the following output:

S Diff: 1.131... (keeping your level of precision)
D Diff: 1.109... (keeping your level of precision)

This shows that double quotes are faster?!

I'll happily post a video of it happening if anyone disbelieves the
results... I haven't modified the script (except to take out the extra
two } that cause syntax errors).
So, single quotes wins hands down. To compare echo vs print:

Output:
E Diff: 12.626... (echo)
P Diff: 15.637... (print)


E Diff: 1.096...
P Diff: 1.128...

OK, here my results agree with yours (although mine only has it 3% faster
not the 24% yours shows).

Anyway, if the server can throw out 100,000 (near as damn it) prints
(which is way more than any sane script would do) in a second using either
the fastest or slowest method +/-3% then I'll stick to my normal method.

However, are my results unique. Can anyone else run this test besides me
and Carl so I can see if I've got a mean, lean (and unique) PHP machine :-)

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Apr 17 '06 #17

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

Similar topics

2
by: S. Staats | last post by:
Good day, everbody. Here is a simple program called test.py: #!/usr/bin/python print "No newline, please.", # End of program Here is what program does: prompt> ./test.py No newline, please.
9
by: Alan Mackenzie | last post by:
To all those who use (X)Emacs's CC Mode to edit C, C++, Java, Objective-C, Pike, AWK or IDL: To help direct the development of CC Mode, it would be useful to find out how people use the...
5
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
5
by: Kaka | last post by:
Hi, I am a learner for C# and .Net. I want to know how to put a line break to a string so that when it show in the textbox the text will be displayed in saperated lines. e.g.: String s; s...
29
by: runningdog | last post by:
Hi, I would like to be able to embed a newline in a text string. Is there any convienent notation to do this TIA Steve
7
by: Alex Nordhus | last post by:
I am looking for a way to strip the blank line and the empty newline at the end of the text file. I can get the blank lines removed from the file but it always leaves the end line (which is blank)...
4
by: Peter Kirk | last post by:
Hi I would like to ask a little bit about the value Environment.Newline: what is it and what is the point of it? Ok, I can see in the docs that it represents "newline" for the current platform -...
5
by: Adam Right | last post by:
Hi, Is there a way to construct the mail body including newline characters by using .net framework mailing functions when sending an email? I cannot insert newline character into the body of the...
11
by: rossum | last post by:
I want to declare a const multi-line string inside a method, and I am having some problems using Environment.NewLine. I started out with: class foo { public void PrintStuff() { const...
1
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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...
0
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...
0
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...
0
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...
0
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...

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.