php tutorial - PHP String Heredoc - php programming - learn php - php code - php script




  • The string heredoc methodology is used to create fairly complex strings which is mentioned in double quotes (“ “).
  • The heredoc supports all the features of double quotes and allows creating the string values with more than one line without concatenations.
  • Using double quotes, if we create strings that have multiple lines it will generate errors.
  • In terms of php heredoc starts with “<<<” symbol with the user defined name.
  • The heredoc statement ends with the user defined name terminated by the semicolon (;).
  • On using the heredoc statement it will defined as the double quoted (““) string that is escape characters are interpreted.

Example for heredoc php tutorials Syntax :

echo <<<identifier     // user defined name
content……
identifier;
click below button to copy the code. php tutorial - team

php 7 Sample Code : sample php program

<!DOCTYPE html>
<html>
    <body>
        <?php
            $var=10;
            echo <<<Sample
            This is a heredoc string and here we call the variable $var.
            But it will we be take the value of the variable,
            And also Newlines and everything else is preserved.
            Sample;
        ?>
    </body>
</html>
click below button to copy the code. php tutorial - team

php program Code Explanation :

Code Explanation for String Heredoc In PHP

  1. Here we defined the variable $var1 with its value as 10.
  2. In this Example the heredoc string will be defined in the echo statement and it starts “<<<” with the symbol along with user defined name “Sample” .
  3. Inside the echo statement we specified multiple text strings which will be displayed in the browser as shown in the output.
  4. The heredoc statement is terminated with the user defined name with semicolon (;).

php coding Sample Output :

output for String Heredoc In PHP

  1. Here in this output statement, variable name “$Ten” defines the string as heredoc so the output will be displayed after the interpretation of the variable “$Ten” whose value is “10” which will be assigned, where the newline spaces will be removed as shown above.


Related Searches to php string heredoc