Forum Moderators: coopster
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
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?
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]
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.
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;
?>
$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
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...]
$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.
...
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
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
At one point I was using single quotes in variables to avoid escaping them but heredoc seemed better. To each, his own...
Birdman