Problem with return of car in PHP

1

I have a problem writing in a text file that when I apply the \ n does not pass to the next line and it leaves all the text pasted, here the code.

$Data = "Nombre " . $jsonencode[0]->nombre . "\n" . $jsonencode[0]->
apellidos . "\n". $jsonencode[0]->email;
    
asked by Brian Hernandez 23.06.2018 в 19:23
source

2 answers

2

Since the line break could vary according to the platform, it would be best to use the predefined constant PHP_EOL .

  

[Prints] the correct 'End of Line' symbol of the platform in use.   Available since PHP 5.0.2

     

Predefined Constants in the PHP Manual

In this way you delegate PHP to use the appropriate line break according to the platform where the program is run.

The code would look like this:

$Data = "Nombre " . $jsonencode[0]->nombre .PHP_EOL. $jsonencode[0]->
apellidos . PHP_EOL. $jsonencode[0]->email;

By doing that, you will not have to worry about whether it will work on some platforms and not on others.

The only restriction is that it works as of PHP 5.0.2. If you have an earlier version, you could implement a function that asks about the operating system / platform and depending on that, decide which line to use.

    
answered by 24.06.2018 / 00:58
source
2

Different operating systems use different conventions for line breaks, Windows for example, uses "\ r \ n", and "\ n" is used in unix systems. Therefore, if you are working on Windows you can try replacing the "\ n" with "\ r \ n". For more details, check the notes section of the documentation.

    
answered by 23.06.2018 в 21:15