Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #16826

Re: echo "<br>" wouldn't print a new line

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.php
Subject Re: echo "<br>" wouldn't print a new line
Date 2016-07-05 21:43 +0200
Organization PointedEars Software (PES)
Message-ID <11587625.uLZWGnKmhe@PointedEars.de> (permalink)
References <83e11700-3227-43a8-bf36-ab77004976e6@googlegroups.com> <nldd7c$so7$1@solani.org>

Show all headers | View raw


Christoph M. Becker wrote:

> On 04.07.2016 at 12:06, Alla wrote:
>> I am currently learning C programming, and still a newby, but I have also

_newbie_

>> started php. I will be grateful for your help.

_PHP_

>> I have watched a tutorial for beginners on using some functions, and the
>> author used echo "<br>"; command to print a new line for the next output.
               ^^^^^^^^^^^
>> I have tried it, but it didn't work.
>> 
>> What could be the problem?
>> 
>> […]
> 
>> and here is how the output looks on my Termnal - the "first" is not being
>> printed on a new line:
> 
> `echo` just prints the arguments it's given.  There's no magic involved.

There is, literally, a little bit of magic involved: “echo” and “print” 
print their respective arguments as if they had been typecast to string.  
That is, if you are “echo”ing or “print”ing an object, and that object 
inherits a “magic” __toString() method, then the return value of that method 
(typecast to string if necessary), is printed:

$ php -r 'class C { function __toString () { return "42"; }} echo new C(); 
echo PHP_EOL;'
42

$ php -r 'class C { function __toString () { return "42"; }} print new C(); 
echo PHP_EOL;'
42

The difference between “echo” and “print” is that “print” returns 1 and can 
be used as if it were a function.

<http://php.net/manual/en/language.oop5.magic.php#object.tostring>
<http://php.net/echo>
<http://php.net/print>

>  `echo <br>` will never print a new line.

Well, to begin with, that is _not_ what *they* used.

>  Only when that is part of a HTML document, the browser will display a
>  line break.  […]

No, the way *you* wrote it, it is always a syntax error (the backticks 
notwithstanding).

> ["\r", "\n", "\r\n", or PHP_EOL instead of '<br>']

You are right about that of course, but ISTM that you are missing the point:

Probably the OP is unaware that PHP can run in different environments, and 
that the tutorial they are using contains exercises for the environment of a 
HTML user agent as that is the most common way to use PHP, the PHP 
*Hypertext* Preprocessor.  Whereas the OP has attempted to run the code 
using the Command-Line Interface (CLI) version of PHP since they are 
accustomed to executing programs on the command line from the (little) C 
development experience they have.

When one needs to convert newlines to HTML line breaks, 
<http://php.net/nl2br> comes in handy.  [However, one should avoid 
generating consecutive “br” elements for paragraphs; paragraphs is what the 
“p” element is for instead.]
 
-- 
PointedEars
Zend Certified PHP Engineer 
<http://www.zend.com/en/yellow-pages/ZEND024953> | Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

echo "<br>" wouldn't print a new line Alla <modelling.data@gmail.com> - 2016-07-04 03:06 -0700
  Re: echo "<br>" wouldn't print a new line "R.Wieser" <address@not.available> - 2016-07-04 12:14 +0200
  Re: echo "<br>" wouldn't print a new line "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-07-04 12:19 +0200
    Re: echo "<br>" wouldn't print a new line Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-07-05 21:43 +0200
      Re: echo "<br>" wouldn't print a new line "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-07-06 13:49 +0200
        Re: echo "<br>" wouldn't print a new line Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-07-06 20:57 +0200
  Re: echo "<br>" wouldn't print a new line Alla <modelling.data@gmail.com> - 2016-07-04 09:38 -0700
  Re: echo "<br>" wouldn't print a new line Tim Streater <timstreater@greenbee.net> - 2016-07-04 12:30 +0100
  Re: echo "<br>" wouldn't print a new line Arno Welzel <usenet@arnowelzel.de> - 2016-07-09 17:43 +0200
    Re: echo "<br>" wouldn't print a new line Sebastiaan Kop <sebastiaan@famkop.nl> - 2016-07-24 16:14 +0000
      Re: echo "<br>" wouldn't print a new line Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-07-30 14:03 -0400
        Re: echo "<br>" wouldn't print a new line Jivanmukta <jivanmukta@poczta.onet.pl> - 2016-07-30 20:08 +0200
          Re: echo "<br>" wouldn't print a new line Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-07-30 17:18 -0400
        Re: echo "<br>" wouldn't print a new line "R.Wieser" <address@not.available> - 2016-07-31 13:28 +0200
          Re: echo "<br>" wouldn't print a new line "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-07-31 15:28 +0200
            Re: echo "<br>" wouldn't print a new line "R.Wieser" <address@not.available> - 2016-07-31 17:38 +0200
              Re: echo "<br>" wouldn't print a new line "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-08-01 14:57 +0200
                Re: echo "<br>" wouldn't print a new line "R.Wieser" <address@not.available> - 2016-08-01 15:18 +0200
      Re: echo "<br>" wouldn't print a new line "J.O. Aho" <user@example.net> - 2016-07-31 08:17 +0200

csiph-web