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 Create a New Line in JavaScript ?
Next article icon

How to create a New Line in PHP?

Last Updated : 06 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

How to create a New Line in PHP?

Below are the following ways by which we can create a new line in PHP.

1. Using Line Breaks as in HTML

The <br> tag in HTML is used to give a single-line break. It is self-closing and doesn’t require an end tag.

Syntax:

<br>

Example: 

PHP
<?php echo "Hello, World!<br/>"; echo "This is a new line.<br>"; ?> 

Output
Hello, World! .<br/>This is a new line.<br>

2. Using new line Tags(\n or \r\n)

Newline characters like \n (for UNIX/Linux) or \r\n (for Windows) are used to create new lines in text-based output, such as in CLI or text files.

Syntax: 

 "\n"

Example 1: 

PHP
<?php echo "Hello, World!\n"; echo "This is a new line.\n"; ?> 

Output
Hello, World! This is a new line. 

Syntax: 

"\r\n"

Example 2: 

PHP
<?php echo "Hello, World!\r\n"; echo "This is a new line.\r\n"; ?> 

Output
Hello, World! This is a new line. 

3. Using nl2br() Function in PHP

The nl2br() function is used in PHP to convert newline characters (\n) into HTML <br> tags, which helps preserve formatting when displaying text on a webpage.

Syntax:

nl2br(string $string, bool $is_xhtml = true): string

Example:

PHP
<?php $text = "Hello, World!\nThis is a new line."; echo nl2br($text); ?> 

Output
Hello, World!<br /> This is a new line. 

Best Practices

  • Use <br> for HTML: Always use <br> for line breaks in HTML content.
  • Avoid \n in HTML: Don’t use \n in HTML output; it won’t create visible line breaks.
  • Use nl2br() for Text with Newlines: Use nl2br() to convert newlines (\n) into HTML <br> tags when displaying text on web pages.
  • Understand Browser Behavior: Browsers ignore extra spaces and newlines in HTML. Always use <br> for visible line breaks.

Conclusion

In PHP, creating new lines is essential for organizing and formatting text output. Depending on your environment (web, command-line, or file-based output), you can use different methods to insert line breaks.



Next Article
How to Create a New Line in JavaScript ?

A

akshitsaxenaa09
Improve
Article Tags :
  • PHP
  • Web Technologies
  • HTML-Tags
  • PHP-Questions

Similar Reads

  • 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 Create a New Line in JavaScript ?
    A new line can be made in JavaScript using the below-discussed methods. Using Escape Sequence `\n`The \n escape sequence represents a newline character in JavaScript strings and it can used to render a new line in JavaScript. [GFGTABS] JavaScript console.log("GfG"); console.log("\n
    3 min read
  • How to insert a line break in PHP string ?
    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
    2 min read
  • How to add new line in Ruby?
    In Ruby, newlines (line breaks) play a crucial role in formatting and separating your code. Whether you're crafting clear error messages or building readable output, knowing how to add newlines is essential. Here's a breakdown of various methods you can use: 1. The Almighty `puts`:The built-in `puts
    2 min read
  • How to add a new line in the alert box ?
    Sometimes we need to show more than one line in an alert box so that we can convey our message in a great form. These are the following methods: Table of Content Using "\n" escape characterUsing "\r" escape characterUsing template literalMethod 1: Using "\n" escape characterIn this approach, we are
    2 min read
  • How to Insert a New Element in an Array in PHP ?
    In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key. We can insert an element or item in an array using the below functio
    5 min read
  • How to Add New Line in Dictionary in Python
    Dictionaries are key-value stores that do not inherently support formatting like new lines within their structure. However, when dealing with strings as dictionary values or when outputting the dictionary in a specific way, we can introduce new lines effectively. Let's explore various methods to add
    3 min read
  • How To Add Line Break in CSS?
    CSS provides flexible options to add or manage line breaks in your content. Here are some effective techniques: 1. Using the white-space PropertyThe white-space property allows controlling how text wraps and respects line breaks within an element. [GFGTABS] HTML <html> <head> <style
    2 min read
  • How to read a Large File Line by Line in PHP ?
    We will use some file operations to read a large file line by line and display it. Read a file: We will read the file by using fopen() function. This function is used to read and open a file. Syntax: fopen("filename", access_mode); Parameter: filename: Filename is the name of the file access_mode: I
    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