php

Use of Heredoc in PHP

Use of Heredoc in PHP
Heredoc is one of the ways to store or print a block of text in PHP. The data stored in the heredoc variable is more readable and error-free than other variables for using indentation and newline. How the heredoc content can be stored in a variable or printed has shown in this tutorial.

Defining the heredoc document

The following steps need to follow to store or print the heredoc document.

  1. '<<<' is used to start the heredoc document.
  2. A delimiter is required to use after '<<<' to define the starting of the document and the same delimiter name with a semicolon(;) is used at the end of the heredoc document to define the end of the document.

Example 1: Printing heredoc content

The following example shows the uses of two heredoc documents. Create a PHP file with the following script. In the script, a long text is printed using a heredoc document. The newline used in the first heredoc content does not generate a newline in the browser. ,

 tag is used with the second heredoc document to print the heredoc content as defined in the editor.

//Print the first heredoc document
print <<< HERE
PHP is a general-purpose scripting language especially suited to web development.
It was created by Danish-Canadian programmer Rasmus Lerdorf in 1994.
The PHP reference implementation is now produced by The PHP Group.
HERE;
//Print the second heredoc document
print <<< DOC

www.google.com
www.bing.com
www.ask.com
www.yahoo.coms

DOC;
?>

Output:

The following output will appear after running the above script from the server.

Example 2: Using heredoc content in a variable

The following example shows how the heredoc content can be stored in a variable and print with other variables. Create a PHP file with the following script. $name and $phone variables are used here to store string values. $address variable is used to store heredoc content. Next, these three variables are printed by combining them.

//Define a string variable
$name = 'Carol J. Stephens';
//Define a heredoc variable
$address = <<< addr

1635, Franklin Street Montgomery,
AL 36104.

addr;
//Define another string variable
$phone = '126-632-2345';
//Print the variables
echo "Name :
   $name 
". "Address : $address". "Phone :
    
$phone
";
?>

Output:

The following output will appear after running the above script from the server. The contents of the variables are printed as defined in the script for using the

 tag.

Example 3: Displaying HTML form using heredoc variable

The following example shows how the HTML form can be defined in a variable by using the heredoc document. Create a PHP file with the following script.  A login form is designed using a heredoc document and stored in the variable, $form. The $form is printed to display the login form. Next, the PHP script will check the username and password are valid or invalid. The script will print the success message for valid entry and the error message for invalid entry.

//Define the login form
$form = <<< HTML









html;
echo "

Login Form

";
//Display the login form
echo $form;
//Check the submit button is clicked or not
if(isset($_POST['submit']))

//Check the validity og the user
if($_POST['username'] == 'admin' && $_POST['password'] == 'secret')
echo "Authenticated user";

else
echo "Username or password is wrong.";


?>

Output:

The HTML form will display after running the script from the server. The output shows the error message, 'Username or password is wrong' for the invalid entry.

If the user types admin as username and secret as password the script will print the success message. The following output shows the success message, 'Authenticated user' for typing the valid username and password.

Example 4: Using the variable inside the heredoc content

The following example shows how any variable can be used inside the heredoc content. Create a PHP file with the following script. A variable named $website is initialized with a string value that is used inside the heredoc content in the script. Next, the heredoc variable, $var is printed with formatting.

//Declare a variable with string value
$website = 'LinuxHint';
//Use variable in the heredoc content
$var = <<$website is a popular blog site.
here;
//Print the heredoc variable
echo "

". $var ."

";
?>

Output:

The following output will appear after running the above script from the server.

Example 5: Using heredoc variable inside the function

The following example shows how the argument values of a function can be used in a heredoc content. Create a PHP file with the following script. Here, the user-defined function named display() will take two values by two argument variables when it will call and these variables will be used inside the heredoc content. The function is called with two string values at the end of the script.

//Define a user-defined function
function display($book,$author)

//Use the argument values inside the heredoc content
print <<

Book Name: $book

Author Name: $author

Publisher: O'Reilly

book;

//Call the function
display("Head First PHP & MySQL","Lynn Beighley and Micheal Morrison");
?>

Output:

The following output will appear after running the above script from the server. “Head First PHP & MySQL” is passed in the first argument and “Lynn Beighley and Micheal Morrison” is passed in the second argument of the display() function. The output shows the formatted heredoc content with the values of the argument values.

Conclusion

heredoc is a good feature of PHP for storing and printing long text with any HTML tag or other variables. newdoc is another feature of PHP like heredoc that released after PHP version 5. This tutorial shows the different uses of heredoc documents in PHP by using simple examples to help the readers to know the way of using heredoc in PHP script.

How to change Left & Right mouse buttons on Windows 10 PC
It's quite a norm that all computer mouse devices are ergonomically designed for right-handed users. But there are mouse devices available which are s...
Emulate Mouse clicks by hovering using Clickless Mouse in Windows 10
Using a mouse or keyboard in the wrong posture of excessive usage can result in a lot of health issues, including strain, carpal tunnel syndrome, and ...
Add Mouse gestures to Windows 10 using these free tools
In recent years computers and operating systems have greatly evolved. There was a time when users had to use commands to navigate through file manager...