What is EOT in PHP
Define strings with HEREDOC syntax (Theme: PHP examples)
The following article is largely the same as that about the NOWDOC syntax. If you have already read the latter, it is sufficient to skim over this roughly.
HEREDOC is a special syntax in PHP for marking longer, often multiline, sections of text. They are introduced by “<<< MARKING”, whereby “MARKING” can be any string. As a rule, it is capitalized to improve clarity. “<<< MARKING” must be followed by a line break and then the text. This can be written in the same way as between double quotation marks. Variables are also automatically recognized by PHP here and replaced by their values. In contrast to double quotation marks, however, double quotation marks can also be placed within the string without them having to be escaped. The end of the HEREDOC section is marked with "MARKER;" which must be at the beginning of a new line.
In addition to the HEREDOC syntax, there is also the NOWDOC syntax, which is explained in a separate article. While variables are parsed in the strings with HEREDOC, this is not the case with NOWDOC. HEREDOC is similar to the string definition using double quotation marks, NOWDOC is more like the one using single quotation marks.
3. Example: A simple string in HEREDOC syntax
In this example, $ heredoc is defined as a string with the content “abcdefg” using the HEREDOC syntax.
4. HEREDOC with several lines
Similar to the previous example, but this time the string spans several lines.
5. HEREDOC and indentations
In this example, indents are made using tabs. As can be seen in the output, these are also adopted. If you want to avoid this, you must not indent any lines in HEREDOC.
6. HEREDOC with different start and end characters
In the next example, different names are chosen for the start and end markers. The naming follows the same rules as for variables: lower and upper case letters, numbers and underscores are allowed. However, the name cannot begin with a number.
7. HEREDOC with the start mark in quotation marks
Optionally, the start marker can also be placed in double quotation marks. This makes the HEREDOC syntax more similar to NOWDOC, as single quotation marks are required for the latter.
8. Access to variables in HEREDOC
Within HEREDOC, variables are automatically replaced by their values, as is known from the string definition with double quotation marks.
9. Use quotation marks within the text
HEREDOC shows its strengths with multi-line texts, which can also contain quotation marks. Overall, the text is easier to read than with a normal definition using single or double quotation marks.
10. The start and end markings may be included in the text
The characters from the start and end markings may be part of the text. Placement at the beginning of a line is also permitted. In the latter case, however, at least one further character must follow the character string, as it is not mandatory in HEREDOC to end the HEREDOC section with a semicolon.
In a new line, without any further character (generates an error):
11. HEREDOC can lead to unsightly code
At the top or global level in the code, the HEREDOC syntax is nice to look at, but with deeper nesting it can lead to unsightly code, since the text and the end marker should or must be at the beginning of the line.
12. HEREDOC for fields of classes and class constants
Just like for the definition of "normal" variables, HEREDOC can also be used to set fields and constants of a class.
13. Use HEREDOC as a parameter
Strings defined via HEREDOC do not necessarily have to be assigned to variables. Instead, it is also possible to use them directly as parameters. This is shown below by transferring a string defined via HEREDOC to echo ().
In this second example, a HEREDOC string is passed to var_dump, similar to the previous example.
14. Line breaks before the end of HEREDOC
This example analyzes how HEREDOC treats line breaks directly before the end marker. To do this, three line breaks are set after the last character. As can be seen, the very last line break is truncated, the other two are retained.
15. Space before the end of HEREDOC
Analogous to the previous example, we will now examine how HEREDOC treats spaces before its end. This time, three spaces are placed before the last line break (which must be set). As can be seen in the output, HEREDOC does not remove the spaces.
16. The marking of the end must be in a new line
In this example, the end marker is not set at the beginning of a new line as a test. The result is a parse error. The end marker must always be at the beginning of a line.
17. HEREDOC with error suppression
If array keys are to be accessed within the HEREDOC section that are potentially not defined, warnings can be suppressed by using the @ symbol. This must be placed directly in front of the start marker (“@ <<< MARKING”). It should be noted that this is an ugly style as the warnings are still generated. The @ sign just means that they are no longer displayed.
The same example without error suppression (only the @ is missing):
Notice : Undefined index: username in ... \ test.php on line 4
Hello, it is an error occurred with the following message: A sample problem has occurred. Location of the error: index.php, line 127123.
- What are the problems of Belarus
- Harry Potter was technically not a pure blood
- What are the universal laws of life
- Hiring TCS civil engineers
- What makes fat thicker than oil
- What are the memories of Robert Mugabe
- How is the capacitance created in a diode
- What are some advantages of a drought
- What makes llamas spit?
- What are Hillary Clinton's most famous quotes
- All payments are in ClickBank PPC
- Who started monetary policy
- Why does everyone hates poetry
- Can we eat coriander leaves and onions?
- What is a tarantula food chain
- Do Docker containers remember previously executed commands
- May the girl Christopher Nolan
- Is toilet paper dirty
- Which bird name ends with ron
- What are the best development tools now
- Who are SABIC's top 3 competitors
- How is biotechnology on the Amity Noida campus
- What is CQRS
- Can a person track B Ed