user6214813
user6214813

Reputation:

php \r - print statements before and after not working

The print statements before and after a print statement containing a Carriage return are not working.

<?php
print "hello";
print " hai \r";
print "bro";
?>

the output is

bro

Upvotes: 0

Views: 130

Answers (2)

ConstantineUA
ConstantineUA

Reputation: 1051

Everything works fine, this symbol is called carriage return and it's used to reset a cursor position.

So what your script is doing:

  1. print a line
  2. print a line and reset cursor position
  3. print a line again

You see this behaviour only while running a script in console.

The better way to have a new line symbol in your code is to use a special constant PHP_EOL as it will use a proper sequence (\r or \n\r or \n) depending on a running platform.

Upvotes: 1

Aiken
Aiken

Reputation: 262

Use

<?php
print "hello";
print " hai \r\n";
print "bro";
?>

For a new line.

Upvotes: 0

Related Questions