Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
How to Insert a Line Break VARCHAR String in MySQL
Next article icon

How to insert a line break in PHP string ?

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will discuss how to insert a line break in a PHP string. We will get it by using the nl2br() function. This function is used to give a new line break wherever '\n' is placed.

Syntax:

nl2br("string \n");

where the string is the input string.

Example 1: PHP Program to insert a line break in a string.

PHP
<?php  // Insert a new line break echo nl2br("This is first line \n This is second line");  ?> 

Output
This is first line <br />  This is second line

Example 2:

PHP
<?php  // Insert a line break with new line echo nl2br("This is first line \n This is second "    + "line \n This is third line \n This is fourth line");  ?> 

Output
This is first line <br />  This is second line <br />  This is third line <br />  This is fourth line

Using Nowdoc Syntax

Using Nowdoc syntax in PHP, you can insert a line break by defining a multi-line string. This syntax treats everything literally, without parsing variables or special characters.

Example:

PHP
<?php $string = <<<'EOD' Hello, World! EOD; echo $string . PHP_EOL; // Outputs to the console ?> 

Output
Hello, World! 

Using PHP_EOL Constant

The PHP_EOL constant in PHP represents the end-of-line character appropriate for the platform PHP is running on (e.g., "\n" on Unix-like systems, "\r\n" on Windows). You can use this constant to insert platform-independent line breaks in a string.

Example:

PHP
<?php function insertLineBreakUsingEOL($string) {     return str_replace("\n", PHP_EOL, $string); }  $inputString = "Hello,\nWorld!"; $formattedString = insertLineBreakUsingEOL($inputString);  echo nl2br($formattedString);   ?> 

Output
Hello,<br /> World!

Using str_replace()

The str_replace() function in PHP can replace all occurrences of a substring within a string. You can use this function to replace the newline character \n with <br /> tags for HTML output.

Example:

PHP
<?php function insertLineBreakUsingStrReplace($string) {     return str_replace("\n", "<br />", $string); }  $inputString = "This is the first line \nThis is the second line"; $formattedString = insertLineBreakUsingStrReplace($inputString);  echo $formattedString; ?> 

Output
This is the first line <br />This is the second line 



Next Article
How to Insert a Line Break VARCHAR String in MySQL
author
gottumukkalabobby
Improve
Article Tags :
  • Web Technologies
  • PHP
  • PHP-string
  • PHP-function
  • PHP-Questions

Similar Reads

  • How to insert a line break in a String PL/SQL
    In PL/SQL, inserting line breaks into VARCHAR or NVARCHAR strings can be a good way to enhance the readability and presentation of our data. Whether we are formatting output for display or preparing text for storage, knowing how to insert line breaks is a helpful skill. Here, we will explore techniq
    5 min read
  • How to Insert a Line Break in a String PostgreSQL ?
    In PostgreSQL, managing strings efficiently is crucial, especially when dealing with VARCHAR and NVARCHAR data types. One common challenge developers face is inserting line breaks within these strings. In this article, we'll explore different approaches to tackle this issue, allowing us to format ou
    5 min read
  • How to Insert a Line Break VARCHAR String in MySQL
    When dealing with textual data in MySQL databases, maintaining proper formatting is important for readability and clarity. One common formatting requirement is to insert line breaks within VARCHAR and NVARCHAR strings. In this article, we will understand How to insert a line break in a MySQL VARCHAR
    3 min read
  • How to Insert Line Break in SQL Server String?
    In SQL Server there are various datatypes like int, float, char, nchar, etc but especially while we are dealing with text in VARCHAR and NVARCHAR columns, we might run into situations where we need to make the text look cleaner by adding line breaks. This could be for better organization, and readab
    4 min read
  • How to Insert a Line Break in a SQLite VARCHAR String
    SQLite is a popular relational database management system known for its simplicity and flexibility. However, inserting a line break in a VARCHAR/NVARCHAR string can be challenging due to SQLite's limited support for special characters. In this article, we will explore different approaches to inserti
    3 min read
  • How to Insert a Line Break in a SQL VARCHAR
    In SQL, VARCHAR and NVARCHAR are widely used data types for storing character data. It may sometimes be necessary to insert line breaks into these string values ​​for better readability or to display the information in a formatted manner. Although SQL itself does not have a specific newline characte
    3 min read
  • How to Insert New Line in R Shiny String
    Inserting a new line in a string within an R Shiny application is a common task, especially when dealing with text outputs in UI components such as text Output, verbatimTextOutput, or even within HTML tags. Understanding how to insert new lines correctly helps improve the readability and organizatio
    4 min read
  • How to convert an Integer Into a String in PHP ?
    The PHP strval() function is used to convert an Integer Into a String in PHP. There are many other methods to convert an integer into a string. In this article, we will learn many methods. Table of Content Using strval() function.Using Inline variable parsing.Using Explicit Casting.Using sprintf() F
    3 min read
  • How to create a New Line in PHP ?
    When working with text output in PHP, like creating a webpage, sending an email, or saving to a file, it's important to know how to add a new line. Adding a new line helps break up the text, makes it easier to read, and keeps it looking right whether it's shown in a browser, a terminal, or a file. H
    2 min read
  • How to set a single line break in HTML5 ?
    In this article, we will add a single line break using the <br> tag. It s used to give the single line break. It is the empty tag so it does not contain end tag. Syntax: <br> Example 1: This example uses <br> tag to break the line. <!DOCTYPE html> <html> <head>
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences