-4

Description:

I have the follwoing code:

        if(isset($riskTriggerFlag) && $riskTriggerFlag==1) // if risk_mitigator process reads and offer weight is  maintained based on offer mentioned.
        {
            echo "Risk Weight: ".$risk_weight ."\n";
            $randNo = rand(1,100);

            echo "Random Weight: ". $randNo ."\n";
            if($randNo < intval(100 - $risk_weight))
            {
                $flagThrowPostback = 1;
            }
        }

As you can see,I am putting \n in the text,but still the message in getting printed in this matter:

Risk Weight: 20 Random Weight: 20 

What am I doing wrong?

Note:

I can't use br tag or nl2br() since the total page is just a php page, and I don't want to add any html tags.

output on broswer is coming like this:

<html>
<head></head>
<body>Multiple row Risk Weight: 20 Random Weight: 20 </body>
</html>
Jongware
  • 22,200
  • 8
  • 54
  • 100
Saswat
  • 12,320
  • 16
  • 77
  • 156
  • Do you look at the output in the browser? (Probably you do, so you have to use br tags otherwise your web browser won't show you a new line) – Rizier123 Jun 04 '15 at 10:04
  • 3
    [`PHP_EOL`](http://stackoverflow.com/questions/21373478/n-vs-php-eol-vs-br) should work fine. – Andrei Jun 04 '15 at 10:04
  • 1
    So as you look at the output in the browser you have to use `br` tags. Or you put your output into `pre` tags, then you can use your `\n` – Rizier123 Jun 04 '15 at 10:06
  • If you are seeing that in browser, use `nl2br()` – viral Jun 04 '15 at 10:07
  • @Rizier123, I used `\n` already in my code,but still its coming in a single line. ( – Saswat Jun 04 '15 at 10:07
  • 1
    I think better way is to check whether php file is executed from CLI or browser. In case it is from browser then use
    else newline.
    – Elixir Techne Jun 04 '15 at 10:08
  • 1
    @Saswat Did you `echo "
    ";` Before you used your `\n` ?
    – Rizier123 Jun 04 '15 at 10:09
  • @Rizier123, no bro,I didn't use `echo
    `
    – Saswat Jun 04 '15 at 10:10
  • @Saswat Then do it and try it – Rizier123 Jun 04 '15 at 10:11
  • yes, thanks... putting
     did help the problem....
    – Saswat Jun 04 '15 at 10:15
  • I'm voting to close this as whatever the real issue is the OP apparently doesn't want to explain it correctly... – Laurent S. Jun 04 '15 at 10:15
  • @Bartdude, all the people who commented got the clear idea of what I wanted, yet you think that's its not explained enough.... that's strange – Saswat Jun 04 '15 at 10:16
  • @Bartdude, I am reading this page currently on browser,but later it wil be run via commandline method.. That's why I wanted it with free of tags.... for current scenario, to complete my testing with message, I compromised with the little help I am getting – Saswat Jun 04 '15 at 10:28
  • 1
    We can't guess this out of your original question... My suggestion now you explained this, and if you don't want any extra tag (even `
    `), would be to go for a javascript solution based on [this one](http://stackoverflow.com/questions/784539/how-do-i-replace-all-line-breaks-in-a-string-with-br-tags), targetting only text contained in specific parent, which can be filtered based on css class for example. You could also go for a "test mode" in PHP with a flag activated by some querystring parameter for example, which would be conditionnally adding some `nl2br()`calls when ran in test mode
    – Laurent S. Jun 04 '15 at 11:55

1 Answers1

0

I think better way is to check whether php file is executed from CLI or browser. In case it is from browser then use
else newline.

$newline = "<br />";
if (PHP_SAPI === 'cli')
{
   // ...
   $newline = "\n";
} 
if(isset($riskTriggerFlag) && $riskTriggerFlag==1) // if risk_mitigator process reads and offer weight is  maintained based on offer mentioned.
    {
        echo "Risk Weight: ".$risk_weight . $newline;
        $randNo = rand(1,100);

        echo "Random Weight: ". $randNo . $newline;
        if($randNo < intval(100 - $risk_weight))
        {
            $flagThrowPostback = 1;
        }
    }
Elixir Techne
  • 1,848
  • 15
  • 20