{"id":22532,"date":"2024-06-29T06:38:49","date_gmt":"2024-06-29T05:38:49","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/salto-de-linea-en-php-como-realizarlo-de-manera-efectiva\/"},"modified":"2025-10-12T21:00:24","modified_gmt":"2025-10-12T20:00:24","slug":"salto-de-linea-en-php-como-realizarlo-de-manera-efectiva","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/line-break-in-php-how-to-do-it-effectively__trashed\/","title":{"rendered":"Line break in PHP: How to do it effectively"},"content":{"rendered":"<p>When developing web applications using PHP, it is common to find the need to make line breaks in different situations. Whether displaying content on a web page, printing data to a text file, or sending a formatted email, knowing how to perform a line break correctly is essential.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/line-break-in-php-how-to-do-it-effectively__trashed\/#%C2%BFQue_es_un_salto_de_linea_en_PHP\" >What is a line break in PHP?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/line-break-in-php-how-to-do-it-effectively__trashed\/#Salto_de_linea_en_HTML\" >Line break in HTML<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/line-break-in-php-how-to-do-it-effectively__trashed\/#Salto_de_linea_en_un_archivo_de_texto\" >Line break in a text file<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/line-break-in-php-how-to-do-it-effectively__trashed\/#Salto_de_linea_en_un_correo_electronico\" >Line break in an email<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/line-break-in-php-how-to-do-it-effectively__trashed\/#Preguntas_frecuentes\" >Frequently asked questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/line-break-in-php-how-to-do-it-effectively__trashed\/#%C2%BFPuedo_utilizar_rn_en_lugar_de_n_para_realizar_un_salto_de_linea_en_un_archivo_de_texto\" >\u00bfPuedo utilizar &#039;rn&#039; en lugar de &#039;n&#039; para realizar un salto de l\u00ednea en un archivo de texto?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/line-break-in-php-how-to-do-it-effectively__trashed\/#%C2%BFPuedo_realizar_un_salto_de_linea_sin_utilizar_etiquetas_HTML_en_un_correo_electronico_con_PHP\" >Can I make a line break without using HTML tags in an email with PHP?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/line-break-in-php-how-to-do-it-effectively__trashed\/#%C2%BFCuando_debo_utilizar_un_salto_de_linea\" >When should I use a line break?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_un_salto_de_linea_en_PHP\"><\/span>What is a line break in PHP?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A line break refers to a line change within a block of text or code. It is used to visually organize the content, making each new line start at a different point, making it easier to read and understand.<\/p>\n<p>In PHP, there are different ways to perform a line break, depending on the context in which we are working.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Salto_de_linea_en_HTML\"><\/span>Line break in HTML<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If we are generating HTML content dynamically with PHP, we can use the tag &lt;br&gt; to perform a line break. By placing this tag in the PHP code, the browser will interpret that it should change the line when displaying the content in the browser.<\/p>\n<p>For example, if we want to display a greeting with a line break in HTML using PHP, we could use the following code:<\/p>\n<pre>\n&lt;?php\n    $saludo = &quot;&iexcl;Hola Mundo!&lt;br&gt;Bienvenidos a mi p&aacute;gina web.&quot;;\n    echo $saludo;\n?&gt;\n<\/pre>\n<p>When you run this code, the browser will display the text &quot;Hello World!&quot; in one line and &quot;Welcome to my website.&quot; in the next.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Salto_de_linea_en_un_archivo_de_texto\"><\/span>Line break in a text file<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If we are writing data to a text file using PHP, we can use the escape character &quot;n&quot; to perform a line break.<\/p>\n<p>For example, if we want to save the content on separate lines to a text file, we could use the following code:<\/p>\n<pre>\n&lt;?php\n    $archivo = fopen(&quot;miarchivo.txt&quot;, &quot;w&quot;) or die(&quot;Error al abrir el archivo.&quot;);\n    $contenido = &quot;L\u00ednea 1nL\u00ednea 2nL\u00ednea 3&quot;;\n    fwrite($archivo, $contenido);\n    fclose($archivo);\n?&gt;\n<\/pre>\n<p>When opening the file &quot;myfile.txt&quot;, we would find the content separated by lines:<\/p>\n<pre>\nLine 1 Line 2 Line 3\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Salto_de_linea_en_un_correo_electronico\"><\/span>Line break in an email<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If we are sending an email formatted using PHP, we can use the HTML tag &lt;br&gt; within the content of the email to make a line break. This will allow the email to display correctly in the recipient&amp;#039;s email client.<\/p>\n<p>For example, if we want to send a greeting email with a line break in the body using PHP, we could use the following code:<\/p>\n<pre>\n&lt;?php\n    $para = &quot;ejemplo@example.com&quot;;\n    $asunto = &quot;Saludo con salto de l&iacute;nea&quot;;\n    $mensaje = &quot;&iexcl;Hola!&lt;br&gt;&lt;br&gt;Espero que te encuentres bien.&lt;br&gt;Saludos, mi nombre&quot;;\n    $cabeceras = &quot;From: miemail@example.com&quot;;\n    mail($para, $asunto, $mensaje, $cabeceras);\n?&gt;\n<\/pre>\n<p>Upon receiving this email, the recipient will see the content with line breaks correctly formatted, making it easier to read.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Preguntas_frecuentes\"><\/span>Frequently asked questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFPuedo_utilizar_rn_en_lugar_de_n_para_realizar_un_salto_de_linea_en_un_archivo_de_texto\"><\/span>\u00bfPuedo utilizar &#039;rn&#039; en lugar de &#039;n&#039; para realizar un salto de l\u00ednea en un archivo de texto?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>S\u00ed, puedes utilizar &#039;rn&#039; en lugar de &#039;n&#039; para realizar un salto de l\u00ednea en un archivo de texto. Esto es especialmente \u00fatil si deseas que el archivo sea compatible con diferentes sistemas operativos, ya que algunos (como Windows) reconocen &#039;rn&#039; como un salto de l\u00ednea v\u00e1lido.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFPuedo_realizar_un_salto_de_linea_sin_utilizar_etiquetas_HTML_en_un_correo_electronico_con_PHP\"><\/span>Can I make a line break without using HTML tags in an email with PHP?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Yes, you can perform a line break without using HTML tags in an email with PHP. You can use the escape character &quot;rn&quot; within the email content to perform a line break. For example:<\/p>\n<pre>\n$message = &quot;Hello!rnrnI hope you are well.rnGreetings, my name&quot;;\n<\/pre>\n<p>The email will display correctly in the recipient&#039;s email client, with the appropriate line breaks.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFCuando_debo_utilizar_un_salto_de_linea\"><\/span>When should I use a line break?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You should use a line break whenever you want to visually separate lines of text or code. This is especially useful in cases such as generating dynamic HTML content, writing data to text files, or sending emails that require specific formatting.<\/p>\n<p>Remember that using line breaks appropriately improves the readability and organization of your code and content, making it easier to maintain and understand.<\/p>\n<p>With these options, you now have the knowledge needed to perform line breaks in PHP effectively in different contexts. Use this tool to your advantage to improve the presentation and clarity of your code and web content!<\/p>","protected":false},"excerpt":{"rendered":"<p>When developing web applications using PHP, it&#039;s common to encounter the need to use line breaks in different situations. Whether displaying content on a web page, printing data in a text file, or sending a formatted email, knowing how to properly use a line break is crucial.<\/p>","protected":false},"author":1,"featured_media":22533,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420],"tags":[],"class_list":["post-22532","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/22532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=22532"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/22532\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/22533"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=22532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=22532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=22532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}