echo, print and the newline character 
Author Message
 echo, print and the newline character

So I've only been at PHP a few weeks now, but I have an observation or two:

echo and print are the same
the newline character is useless.

If I do this:

<?php
echo "This line is echoed to the browser screen\n";
print "This line is also echoed to the browser screen\n";
?>

the output is:

This line is echoed to the browser screenThis line is also echoed to the browser screen

1. Why have both echo and print ?
2. why support a newline character '\n' ?

anyone, anyone?

- mike



Thu, 03 Nov 2005 07:42:13 GMT  
 echo, print and the newline character
Quote:

> So I've only been at PHP a few weeks now, but I have an observation or two:

> echo and print are the same

They arn't actually the same, but they do the same thing in the end.
echo is a language construct, print is a function.  For example, you can
do $printed=print('Did it work'); and it will return true/false
depending of if it was able to print that text.  You can't do the same
with echo though. Also you can pass a comma separated list of arguments
to echo, but I don't think you can do the same to print.

Quote:
> the newline character is useless.

The new line character is very useful.

Quote:
> If I do this:

> <?php
> echo "This line is echoed to the browser screen\n";
> print "This line is also echoed to the browser screen\n";
> ?>

> the output is:

> This line is echoed to the browser screenThis line is also echoed to the browser screen

Yes, as it should be.  You're miss understanding the use of the new line
character.  If you do a view source, you will see that there is a new
line between the two.  However, the browser treats it as HTML, and in
HTML extra space and new lines are ignored.  You have to use the <br>
tag to get a new line in HTML.


Thu, 03 Nov 2005 02:59:58 GMT  
 echo, print and the newline character

Quote:


>> So I've only been at PHP a few weeks now, but I have an observation or two:

>> echo and print are the same
>They arn't actually the same, but they do the same thing in the end.
>echo is a language construct, print is a function.  For example, you can
>do $printed=print('Did it work'); and it will return true/false
>depending of if it was able to print that text.  You can't do the same
>with echo though. Also you can pass a comma separated list of arguments
>to echo, but I don't think you can do the same to print.

>> the newline character is useless.

>The new line character is very useful.

Maybe I'll see it as I do more php programming, but a line break in an HTML file doesn't seem all that useful.
Can you elaborate?
Quote:

>> If I do this:

>> <?php
>> echo "This line is echoed to the browser screen\n";
>> print "This line is also echoed to the browser screen\n";
>> ?>

>> the output is:

>> This line is echoed to the browser screenThis line is also echoed to the browser screen

>Yes, as it should be.  You're miss understanding the use of the new line
>character.  If you do a view source, you will see that there is a new
>line between the two.  However, the browser treats it as HTML, and in
>HTML extra space and new lines are ignored.  You have to use the <br>
>tag to get a new line in HTML.



Thu, 03 Nov 2005 10:08:11 GMT  
 echo, print and the newline character
Debugging html output without line breaks on a large scale piece of code
would be an absolute nightmare without line breaks.

Some people also like their html to be legible to other people viewing the
site.

PHP isnt just a language for web either, its also used as a general
scripting language, having developed from a command line interpreter.


Quote:
> Maybe I'll see it as I do more php programming, but a line break in an

HTML file doesn't seem all that useful.
Quote:
> Can you elaborate?



Thu, 03 Nov 2005 10:27:43 GMT  
 echo, print and the newline character
Michael,

Quote:

> Maybe I'll see it as I do more php programming, but a line break in an

HTML file doesn't seem all that useful.

Quote:
> Can you elaborate?

The newline character DOES its job as expected. But you are using a browser
to view the output. The browser understands only HTML. So if you want a line
break, you need to use "<br>" not "\n".

If you VIEW SOURCE in your html, the newline does insert a newline
character. Just that your browser does not see this new line unless a <BR>
is included.



Thu, 03 Nov 2005 11:10:24 GMT  
 echo, print and the newline character

Quote:
>> The new line character is very useful.

> Maybe I'll see it as I do more php programming, but a line break in an HTML file doesn't seem all that useful.
> Can you elaborate?

You might neeed a line break for JavaScript.

On the other hand, who tells you, that I want to output HTML with PHP? I
have some programs that output other things (reports, code, ...) and
they need the newline.

    Matthias



Thu, 03 Nov 2005 14:18:55 GMT  
 echo, print and the newline character

Quote:

> Maybe I'll see it as I do more php programming, but a line break in
> an HTML file doesn't seem all that useful.  Can you elaborate?

Apart from the points about making it easier to debug the code that
have already been made, I'll also add that excessively long (many
1000s of characters) lines *occasionally* cause strange browser
behaviour (though clearly they *shouldn't*).

Oh, and \n is also useful if you're using PHP to generate output in a
<pre> tag, or output with Content-type != text/html

--
Chris



Thu, 03 Nov 2005 21:52:57 GMT  
 echo, print and the newline character

See, this is what I needed. My myoptic view of php solely used to generate HTML was my down-fall.

Thinking about I can see the other reasons.

Thanks All for clearing my head

- mike


Quote:

>> Maybe I'll see it as I do more php programming, but a line break in
>> an HTML file doesn't seem all that useful.  Can you elaborate?

>Apart from the points about making it easier to debug the code that
>have already been made, I'll also add that excessively long (many
>1000s of characters) lines *occasionally* cause strange browser
>behaviour (though clearly they *shouldn't*).

>Oh, and \n is also useful if you're using PHP to generate output in a
><pre> tag, or output with Content-type != text/html



Thu, 03 Nov 2005 23:40:52 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Print characters on one line without newline...???

2. PEP 259: Omit printing newline after newline

3. PEP 259: Omit printing newline after newline

4. using echo or print to print out php code

5. the newline character in rule

6. newline character, etc. in smalltalk

7. NewLine character in Clipper

8. Behavior of newline character in ANS Forth, using WRITE-FILE

9. Hard coded newline characters

10. Character input (no echo)....

11. domxml and newline character

12. string.replace() can't replace newline characters???

 

 
Powered by phpBB® Forum Software