-1
<?php
$x="101.5degrees"; 
(double)$x;
print_r($x . '\n'); 
(int)$x; 
echo (string)$x; 
?> 

I expect to print the value of x on a new line.

  • 1
    The codes are incorrect. Initially `$x` is not an array, thus you should not be using `print_r()`. You want to list the `$x` values after you cast it as `double`, `int` and `string`, right? – Raptor Nov 01 '19 at 06:31
  • totally agree with @Raptor – Pardeep Nov 01 '19 at 06:33
  • Yes @Raptor totally agree with you. But even if I change the line print_r($x. '"\n") to `echo $x; echo "\n"` still it is not working. – Mayank Kumbhar Nov 01 '19 at 06:55
  • If I were you, I won't hardcode \n, as line breaks are OS-dependent. Some use \r, some use \r\n. That's why `PHP_EOL` comes to play. – Raptor Nov 01 '19 at 06:57
  • @Raptor just for the sake of understanding as I am using an linux system then \n should work in my case know? Why any escape characters are not working like '\r', '\r\n'? – Mayank Kumbhar Nov 01 '19 at 07:08
  • If you don't specify Content Type as `text/plain`, it's displayed as HTML. In HTML, \n has no visual effects at all. – Raptor Nov 01 '19 at 07:12

2 Answers2

1
$x="101.5degrees"; 
(double)$x;
print_r($x); 
(int)$x; 
echo PHP_EOL.(string)$x;

try this out reference

Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
0

While the other answers didn't really solve OP's problem, here is the correct way of using line breaks in PHP:

<?php
header('Content-Type: text/plain');
$x = "101.5degrees"; 
echo (double)$x . PHP_EOL;
echo (int)$x . PHP_EOL; 
echo (string)$x . PHP_EOL;
?> 

The output of the above code will be:

101.5
101
101.5degrees
Raptor
  • 53,206
  • 45
  • 230
  • 366