Forum Moderators: coopster

Message Too Old, No Replies

print<<<html has never worked for me

         

stidj

7:18 pm on Oct 30, 2003 (gmt 0)

10+ Year Member



Hey guys,

Some time ago I came across some post that says you can drop into html mode within <?> tags

I have seen code (by memory)

<?
print<<<html
<htmltags>
$phpvariable
</htmltags>
end;

?>

Yet when I tried that style I always got a parse error.

Can someone give me a working example using the above style. I like to use raw html in my php scripts and just use variables freely instead of actually embedding php into html.

eg.

<htmltag><? print $somevariable?></htmltag>

thanks

dmorison

7:53 pm on Oct 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep;

[uk.php.net...]

sorry...!

[edited by: dmorison at 8:10 pm (utc) on Oct. 30, 2003]

jatar_k

7:55 pm on Oct 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the little example looks more like heredoc [php.net] than what you are asking for.

Making sure that the page you are putting the php tags on is php parsed, obviously.

You can do exactly as you posted, if you want to just echo a var do
<b><?= $somevar?></b>

Want a little conditional statement?
<input type="text" name="text1" value="<? if (isset($textval)) echo $textval;?>">

build some html stuff with loops
<table>
<?
for ($i=0;$i<3;$i++) {
?>
<tr>
<td>name</td>
<td>value</td>
</tr>
<?
}
?>
</table>

or whatever else, you can embed it a million ways.

That help?

Birdman

7:58 pm on Oct 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, you can...it's heredoc syntax.

Looks like the problem is in your end, it should match the start string:

print <<<HTML
<htmltags>
$phpvariable
</htmltags>
HTML;

You can use it on variables too:

$html = <<<HTML
<htmltags>
$phpvariable
</htmltags>
HTML;

[edited by: jatar_k at 8:00 pm (utc) on Oct. 30, 2003]

[edited by: Birdman at 8:02 pm (utc) on Oct. 30, 2003]

Romeo

7:58 pm on Oct 30, 2003 (gmt 0)

10+ Year Member



... you may try the following.
<?php
print<<<html
<htmltags>
$phpvariable
</htmltags>
html;
?>

The "here document" syntax needs a start <<<XXX and a corresponding end XXX;
so if you start with a "<<<html" you need a closing "html;" on the follwing line after the last line of your desired output. And yes, $variables should get expanded.

Regards,
R.

stidj

8:37 pm on Oct 30, 2003 (gmt 0)

10+ Year Member



Thanks guys but it still does not work. I have tried this before and I still don't see why it will not work. I have tried with and without the space between print<<<HTML (print <<<HTML) and both fail for some reason.

I literally tried to use the code below and it gave me

Parse error: parse error, unexpected T_SL in /home/virtual/site268/fst/var/www/html/test.php on line 3
<?

print<<<HTML
<b>Hi</b>
HTML;

?>

RobinC

9:21 pm on Oct 30, 2003 (gmt 0)

10+ Year Member



I just noticed your error - you need to set a variable, then print the variable (as in Birdman's second example) -

$html = <<<HTML
<htmltags>
$phpvariable
{$complex["phpvariable"]}
</htmltags>
HTML;
echo $html;

Also check your version of php, from the links above -

Note: Heredoc support was added in PHP 4.

I've never been a fan of "print ..." myself, always preferred "echo ..." which must mean something, but dammed if I can think of what ;-)

Robin

dmorison

9:57 pm on Oct 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've never been a fan of "print ..." myself, always preferred "echo ..." which must mean something, but dammed if I can think of what ;-)

[faqts.com...]

RobinC

10:15 pm on Oct 30, 2003 (gmt 0)

10+ Year Member



Actually, think I prefer echo over print becuase every time I type "print" while coding my finger itches to press the "f" afterwards, so I just try not to get confused. I also do some AREXX scripting, where the output func is also called "echo", so I'm semi-used to it anywa :-)

Robin

stidj

10:22 pm on Oct 30, 2003 (gmt 0)

10+ Year Member



<?

$HTML = <<<HTML
<b>Hi</b>
HTML;

?>

The above code still seems to fail.

I am basically looking for equivalent of a PERL script where you can do

print << HTML
<HTMLTAGS>
</HTMLTAGS>
HTML;

That actually does work for me with CGI but with PHP the equivalent

print <<< HTML

HTML;

never works based on examples shown above that I have tested which all give me parse errors for some reason.

MonkeeSage

10:41 pm on Oct 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This works...

... 
echo <<<HTML
This is a <b>test</b>!
HTML;

But notice the closing identifier...

It is very important to note that the line with the closing identifier contains no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs after or before the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by your operating system. This is \r on Macintosh for example.
(Cite [php.net], emphasis original).

Personally, I find it easier to just drop in and out of PHP mode as jatar_k mentioned...

... 
?>
This is a <b>test</b>!
<?php
...

Jordan

stidj

10:45 pm on Oct 30, 2003 (gmt 0)

10+ Year Member



Thanks for the help there.

I agree sometimes dropping in and out of php mode is easier sometimes.

But it makes things messy in some cases and hard to follow and definitely harder to manage in the long run.

I find it much simpler especially when I have a php script where I need to embed a lot of html instead of having to print "HTMLTAGS" to just type them freely.

Of course I realize I could just drop out of php mode but I think it gets to the point where it's harder to tell if you have the right number of closing and opening braces in a huge program.

That's my 2 cents

Birdman

2:18 am on Nov 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I love heredoc and use it alot when looping through database records. It makes it easier when writing the attributes of the HTML tags. I can just go with double quotes and not worry about escaping.

At one point I was using single quotes in variables to avoid escaping them but heredoc seemed better. To each, his own...

Birdman

coopster

12:56 am on Nov 3, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



stidj, if this is still not working, please see the second half of post#7 by RobinC.

What version of PHP are you running?