PHP has introduced the new version in the industry 7.3 which is in trend. The main change is in the existing syntaxes of Heredoc and Nowdoc. PHP Online Training helps the students to learn the basic concepts of Heredoc and Nowdoc.

Regarding Strings

We have various options in PHP to define a string value. Either single or double quote is the two most common approaches. We also have heroic and nowdoc syntaxes that help define a string.

Let's see the example of single and double string before going to Heredoc and Nowdoc.

Double String

$foo = 'kavin';

echo "Hello $foo\nTake Care!";

// Output:-

// Hello Kavin

// Take Care!

Single String

Strings are literal in a single quote.

$foo = 'Kavin';

echo 'Hello $foo\nTake care!';

// Output:- 

// Hello $foo\nTake Care!

Heredoc/Nowdoc

They offer an alternative way to define PHP strings. It is particularly useful when establishing a string across various lines. Have to work by defining an identification at the beginning and end of the string. Any alphanumeric values can be identified by the same variable names.

Let's see the example of Heredoc

echo <<<EOT

Hello Everyone.

Take Care!

EOT;

// Output:-

// Hello Everyone.

// Take Care!

Here EOT is the identifier and any alphanumeric characters can be used for identification purposes. This operator (<<<) is used for the identification purpose. Heredoc is similar to double string and Newdoc is similar to Single string.  E

Example of Newdoc with a single string

$foo = 'kavin';

echo <<<'EOT'

Hello $foo

Take Care!

EOT;

// Output:-

// Hello $foo

// Take Care!

It is important to place the closing ID. Learn PHP Online from Skillsion to enhance your knowledge in Programming. Before PHP 7.3, a new line must always be followed by a half-colon without any white space. Heredoc and nowdoc provide useful options for strings in PHP to the quoted-string syntax. They are particularly useful when defining a string that spans. 

Check out

Why React Native is most effective for Mobile App Development?