Jump to content

Newbie: what's this: $form=<<<form???


OM2

Recommended Posts

It's what is known as here document syntax. Basically it allows you to print out multiple lines of HTML without using lots of echo statements. What they are doing in what you've been reading is putting it all in a variable called $form then echoing out later.

 

Note that you don't have to use <<<form

You can use <<<html or <<<EOF or <<<ABCD

 

As long as the closing identifier (which in your example would have been form;) needs to be the same.

 

Example:

echo <<<html
Hello this is a way
of echoing out lots of stuff
and spacing it out so you
can see it all!
html;

See how it has <<<html then ends with html;

 

I've just echoed it out straight away, but you can store it in a variable. Then add more to it then echo it out.

 

Example:

$string = <<<EOF
Hello this is a wicked string!
EOF;

$variable = "A variable that will be included!";

//NOTICE BELOW HOW I AM ADDING MORE TO THE VARIABLE
$string .= <<<EOF
Now I am adding more, and a variable!
{$variable}
EOF;

//Now that we have all the information, we can echo it!
echo $string;

 

Hope that helps.

thanks.  thats brilliant.

can i stick in "'?

i.e. can i have:

 

echo <<<html

Hello world

This is "really" good

html;

 

let me know.

thanks.

 

actually...

 

whats wrong with:

 

$myVar = "first line

second line

third line";

 

echo $myVar;

 

is the above allowed?

is it the same thing?

does one have an advantage over the other?

thanks.  thats brilliant.

can i stick in "'?

i.e. can i have:

 

echo <<<html

Hello world

This is "really" good

html;

 

let me know.

thanks.

Yes you can.

 

actually...

 

whats wrong with:

 

$myVar = "first line

second line

third line";

 

echo $myVar;

 

is the above allowed?

Yes the above is allowed.

 

is it the same thing?

Basically, but you must escape your quotes with the one above.

 

does one have an advantage over the other?

Here document syntax allows you to leave your quotes un-escaped.

 

You can test this out yourself and try it, it won't hurt. Just get a program like WAMP Server and try a few things out.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.