Current location: Hot Scripts Forums » Programming Languages » PHP » general question about php and html


general question about php and html

Reply
  #1 (permalink)  
Old 05-17-04, 07:18 AM
timfoster timfoster is offline
New Member
 
Join Date: May 2004
Location: Canada
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
general question about php and html

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.

<?php
if (!isset($variable))
{
print "<form name=\"myform\" action=\"post\">";
print "enter name: ";
print "<input type=\"text\" name=\"name\">";
print "<input type=\"hidden\" name=\"variable\" value =1>";
print "<input type=\"submit\">";
print "</form>";
} else {
print "please enter name";
}
?>

thanks in advance for looking at my newbie question
Reply With Quote
  #2 (permalink)  
Old 05-17-04, 07:41 AM
mavoric mavoric is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
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
Reply With Quote
  #3 (permalink)  
Old 05-17-04, 08:22 AM
Klesti Klesti is offline
Newbie Coder
 
Join Date: May 2004
Location: Albania
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
You can also do it in this way :
PHP Code:

<?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; 
Reply With Quote
  #4 (permalink)  
Old 05-17-04, 12:29 PM
timfoster timfoster is offline
New Member
 
Join Date: May 2004
Location: Canada
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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.

Thanks
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP in HTML Pages? ZeoFateX PHP 19 01-04-06 06:42 PM
HTML, ASP.NET, PHP Classes icu HTML/XHTML/XML 1 02-22-04 03:27 PM
php on a html page Bonzo PHP 9 02-06-04 10:40 PM
need help writing script to let user edit thier html or php files pinochio53 PHP 14 12-15-03 07:26 AM
help plz: format retrieved Mysql data in HTML with PHP paulj000 PHP 2 10-19-03 08:03 PM


All times are GMT -5. The time now is 10:35 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.