I'm relatively new to web development and was wondering if someone could give me a good explanation of a coding style I have seen in many php examples.
<?php
if (!isset($variable))
{
?>
<form name="myform" action="post">
enter name:
<input type="text" name="name">
<input type="hidden" name="variable" value =1>
<input type="submit">
</form>
<?php
} else {
print "please enter name";
}
?>
I understand why when the page is initially loaded the form is displayed. I would expect from just looking at the code that each time the submit button is pressed the form would still be displayed as the html is outside of the php tags. I would expect that the interpreter would do something like the ollowing with the php code
if (!isset($variable))
{
} else {
print "please enter name";
}
coding it in the following manner seems (to me anyway) to make more sense as to how it behaves when tested.
i personally prfer the 1st option as i can then see what im doing in dreamweaver, rather than code the whole site (like driving a car with a blindfold on
<?php
if (!isset($variable))
{
echo <<<END
<form name="myform" action="post">
enter name:
<input type="text" name="name">
<input type="hidden" name="variable" value =1>
<input type="submit">
</form>
END;
} else {
echo "please enter name";
}
?>
using echo<<<END or print <<<END makes posible to use quotes under your echo so you don't have to use single quotes. This avoid closing the php tags when using php.
also you can use variables like:
PHP Code:
$form = <<<END
<form name="myform" action="post">
enter name:
<input type="text" name="name">
<input type="hidden" name="variable" value =1>
<input type="submit">
</form>
END;
thanks for your response. I'm still a little confused though.
when you do something like
<?php
if (condition) {
?>
html suff1
<?php
} else {
php print "hello";
?>
why is it that the html coding is only displayed if the condition is true. As it appears to be outside of the php tags I would think that it should be displayed each time. It was my understanding that any html outside of the php code will be displayed each time.