Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write new lines in CLI and web browser?

Tags:

php

I am running a php script from CLI command and web browser. I need to dispaly new lines properly in both ways so that it does not print "<br />" in CLI and it shows new lines in browsers. Does anyone know how to write php function for this?

thanks for any helps

like image 911
TroodoN-Mike Avatar asked Mar 12 '12 10:03

TroodoN-Mike


2 Answers

You could write a function to return the right thing based on the execution environment:

<?php 
if (PHP_SAPI === 'cli') 
{ 
   return PHP_EOL;
} 
else
{
   return "<BR/>";
}
?> 
like image 123
Moo-Juice Avatar answered Sep 17 '22 11:09

Moo-Juice


You can set the output's content type to text/plain to make browsers showing the content as, well, plain text like what you will see on CLI

header("Content-type: text/plain");

Put that before you output anything.

After that, always use \n for new lines

like image 24
fajran Avatar answered Sep 17 '22 11:09

fajran