How to create newline in PHP?

To create a newline, PHP provides nl2br() function. It is an in-built function of PHP, which is used to insert the HTML line breaks before all newlines in the string. Although, we can also use PHP newline character \n or \r\n inside the source code to create the newline, but these line breaks will not be visible on the browser. So, the nl2br() is useful here.

The nl2br() function contains these newline characters \n or \r\n which combinely create the newline. Apart from nl2br() function, a html break line tag </br> is used to break the string. The </br> tag should be enclosed in double quotes, e.g., “</br>”.

For Example

To create the newline in PHP, let’s see the number of examples.

Example 1

Let’s take an of example to create the newline with the help of nl2br() function.

  1. <?php  
  2.     echo nl2br(“New line will start from here\n in this string\r\n on the browser window”);  
  3. ?>  

Output

In this example, we can see that the nl2br() function has contained “\n” and “\r\n” characters in the string for newline. We can see the result of the following program in the below output.

New line will start from here
in this string
on the browser window

Example 2

<?php  
    echo "One line after\n another line";  
    echo " One line after\r\n another line";  
?>  

Output

In the above example, we can see that after “\n” or “\r\n” character, string did not start from the new line. “\n” and “\r\n” alone are not enough for creating a newline in the string, as the whole string is displayed in a single line.

One line after another line One line after another line

Note: The character “/n” is used in Linux to write a newline whereas in windows “\r\n” is used. For the safe side use the “\r\n” character for creating newline instead.

Example 3

<?php  
    echo "One line after\n another line";  
    echo "</br>";  
    echo "One line after\r\n another line";  
?>  

Output

Here, we break the lines using html break line tag “</br>“.

One line after another line
One line after another line

PHP vs HTML

What is PHP?

PHP stands for Hypertext Preprocessor, which is an open source scripting language. It is a server-side scripting language and a powerful tool for creating a dynamic and interactive website.

PHP is an interpreted language, so it doesn’t need compilation. It is specially designed for server-side scripting, which executes on the server. PHP can be easily embedded with HTML files.

Note: PHP is mainly used to develop server-side applications.

It has several advantages that are given below:

  • We can execute PHP code on different platform such as Windows, Linux, UNIX, Solaris, etc.
  • It is easy to use and learn.
  • PHP provides a built-in module which helps it to connect with the database easily.
  • PHP is an open source language that means it is available for free of cost.

In general, PHP is cheap, cross-platform, fast, and reliable to develop web applications.

What is HTML?

HTML stands for Hypertext Markup Language, which is used to create web pages. It is basically used to create static web pages, but it can integrate with CSS, JavaScript, and PHP.

HTML is not a programming language, as it is a tag-based language. Angular brackets <> are used to represents HTML elements or tags.

  • HTML is very easy to learn and implement.
  • It is not a case-sensitive language.
  • We can write HTML code on any text editor like Notepad, Notepad++, Edit plus, etc.
  • HTML is platform-independent, hence, it can be executed on different platform.
  • It allows the programmer to add colors, audio, video, and images on a web page.

Difference between PHP and HTML

PHPHTML
PHP is a server-side programming language.HTML is a client-side scripting language.
PHP is used in backend development, which interacts with databases to retrieve, store, and modify the information.HTML is used in frontend development, which organizes the content of the website.
PHP is used to create a dynamic website. The output will depend on the browser.HTML is used to create a static website. The output of static website remains the same on each time.
PHP can manipulate the data.It cannot manipulate the data.
PHP code executes on web servers like Apache web server, IIS web server.HTML code executes on web browsers like Chrome, Internet Explorer, etc.
PHP is scripting language.HTML is a markup language.
PHP7.3 is the latest version of PHP.HTML5.2 is the latest version of HTML.
PHP is also easy to learn but not as simple as HTML.HTML is easy to learn. It can easily learn in a very short time.
PHP files save with .php extension.HTML files save with .html extension.

PHP Comments

PHP comments can be used to describe any line of code so that other developer can understand the code easily. It can also be used to hide any code.

PHP supports single line and multi line comments. These comments are similar to C/C++ and Perl style (Unix shell style) comments.

PHP Single Line Comments

There are two ways to use single line comments in PHP.

  • // (C++ style single line comment)
  • # (Unix Shell style single line comment)
<?php  
// this is C++ style single line comment  
# this is Unix Shell style single line comment  
echo "Welcome to PHP single line comments";  
?>  

Output:Welcome to PHP single line comments

PHP Multi Line Comments

In PHP, we can comments multiple lines also. To do so, we need to enclose all lines within /* */. Let’s see a simple example of PHP multiple line comment.

<?php  
/* 
Anything placed 
within comment 
will not be displayed 
on the browser; 
*/  
echo "Welcome to PHP multi line comment";  
?> 

Output:Welcome to PHP multi line comment

PHP Data Types

PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:

  1. Scalar Types (predefined)
  2. Compound Types (user-defined)
  3. Special Types

PHP Data Types: Scalar Types

It holds only single value. There are 4 scalar data types in PHP.

  1. boolean
  2. integer
  3. float
  4. string

PHP Data Types: Compound Types

It can hold multiple values. There are 2 compound data types in PHP.

  1. array
  2. object

PHP Data Types: Special Types

There are 2 special data types in PHP.

  1. resource
  2. NULL

PHP Boolean

Booleans are the simplest data type works like switch. It holds only two values: TRUE (1) or FALSE (0). It is often used with conditional statements. If the condition is correct, it returns TRUE otherwise FALSE.

Example:

<?php   
    if (TRUE)  
        echo "This condition is TRUE.";  
    if (FALSE)  
        echo "This condition is FALSE.";  
?>  

Output:

This condition is TRUE.

PHP Integer

Integer means numeric data with a negative or positive sign. It holds only whole numbers, i.e., numbers without fractional part or decimal points.

Rules for integer:

  • An integer can be either positive or negative.
  • An integer must not contain decimal point.
  • Integer can be decimal (base 10), octal (base 8), or hexadecimal (base 16).
  • The range of an integer must be lie between 2,147,483,648 and 2,147,483,647 i.e., -2^31 to 2^31.

Example:

<?php   
    $dec1 = 34;  
    $oct1 = 0243;  
    $hexa1 = 0x45;  
    echo "Decimal number: " .$dec1. "</br>";  
    echo "Octal number: " .$oct1. "</br>";  
    echo "HexaDecimal number: " .$hexa1. "</br>";  
?>

Output:

Decimal number: 34
Octal number: 163
HexaDecimal number: 69

PHP Float

A floating-point number is a number with a decimal point. Unlike integer, it can hold numbers with a fractional or decimal point, including a negative or positive sign.

Example:

<?php   
    $n1 = 19.34;  
    $n2 = 54.472;  
    $sum = $n1 + $n2;  
    echo "Addition of floating numbers: " .$sum;  
?>  

Output:

Addition of floating numbers: 73.812

PHP String

A string is a non-numeric data type. It holds letters or any alphabets, numbers, and even special characters.

String values must be enclosed either within single quotes or in double quotes. But both are treated differently. To clarify this, see the example below:

Example:

<?php   
    $company = "Javatpoint";  
    //both single and double quote statements will treat different  
    echo "Hello $company";  
    echo "</br>";  
    echo 'Hello $company';  
?>

Output:

Hello Javatpoint
Hello $company

PHP Array

An array is a compound data type. It can store multiple values of same data type in a single variable.

Example:

<?php   
    $bikes = array ("Royal Enfield", "Yamaha", "KTM");  
    var_dump($bikes);   //the var_dump() function returns the datatype and values  
    echo "</br>";  
    echo "Array Element1: $bikes[0] </br>";  
    echo "Array Element2: $bikes[1] </br>";  
    echo "Array Element3: $bikes[2] </br>";  
?>  

Output:

array(3) { [0]=> string(13) "Royal Enfield" [1]=> string(6) "Yamaha" [2]=> string(3) "KTM" }
Array Element1: Royal Enfield
Array Element2: Yamaha
Array Element3: KTM

You will learn more about array in later chapters of this tutorial.

PHP object

Objects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared.

Example:

<?php   
     class bike {  
          function model() {  
               $model_name = "Royal Enfield";  
               echo "Bike Model: " .$model_name;  
             }  
     }  
     $obj = new bike();  
     $obj -> model();  
?>  

Output:

Bike Model: Royal Enfield

This is an advanced topic of PHP, which we will discuss later in detail.

PHP Resource

Resources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources. For example – a database call. It is an external resource.

This is an advanced topic of PHP, so we will discuss it later in detail with examples.

PHP Null

Null is a special data type that has only one value: NULL. There is a convention of writing it in capital letters as it is case sensitive.

The special type of data type NULL defined a variable with no value.

Example:

<?php   
    $nl = NULL;  
    echo $nl;   //it will not give any output  
?>

Output:

PHP $ and $$ Variables

The $var (single dollar) is a normal variable with the name var that stores any value like string, integer, float, etc.The $$var (double dollar) is a reference variable that stores the value of the $variable inside it.

To understand the difference better, let’s see some examples.

Example 1

<?php  
$x = "abc";  
$$x = 200;  
echo $x."<br/>";  
echo $$x."<br/>";  
echo $abc;  
?>  

Output:

PHP $ and $$ variables

In the above example, we have assigned a value to the variable x as abc. Value of reference variable $$x is assigned as 200.

Now we have printed the values $x, $$x and $abc.

Example2

<?php  
 $x="U.P";  
$$x="Lucknow";  
echo $x. "<br>";  
echo $$x. "<br>";  
echo "Capital of $x is " . $$x;  
?>  

Output:

PHP $ and $$ variables

In the above example, we have assigned a value to the variable x as U.P. Value of reference variable $$x is assigned as Lucknow.

Now we have printed the values $x, $$x and a string.

Example3

<?php  
$name="Cat";  
${$name}="Dog";  
${${$name}}="Monkey";  
echo $name. "<br>";  
echo ${$name}. "<br>";  
echo $Cat. "<br>";  
echo ${${$name}}. "<br>";  
echo $Dog. "<br>";  
?>  

Output:

PHP $ and $$ variables

In the above example, we have assigned a value to the variable name Cat. Value of reference variable ${$name} is assigned as Dog and ${${$name}} as Monkey.

Now we have printed the values as $name, ${$name}, $Cat, ${${$name}} and $Dog.