|
-
Sep 20th, 2005, 04:51 AM
#1
Thread Starter
PowerPoster
Writing HTML inside of PHP documents..
Hey guys, just wondering how you guys go about writing HTML inside a PHP document? I've been doing it all inside Prints, which means I have to escape the talking marks, so although its probably easier to work with, I find it less readable, and just generally impossible to get it all indented properly for when I view the source in a browser.
PHP Code:
print "<div id=\"loginwrapper\">\n";
print " <div id=\"CreateLoginDiv\">\n";
print " <div id=\"CreateLoginDivTitle\">Please Login:</div>\n";
print " <div id=\"CreateLoginDivContent\">\n";
if($bError) print "<center><div class=\"ErrorBox\"><h4>Error</h4>Invalid Username or Password. Please try again.</div></center>";
print " <form method=\"POST\" action=\"./login.php\">\n
<label>Username<input type=\"text\" name=\"username\" ";
What method do you guys use?
-
Sep 20th, 2005, 05:03 AM
#2
Addicted Member
Re: Writing HTML inside of PHP documents..
For large blocks of HTML code I usually just do this:
PHP Code:
//PHP Code...
?>
<div class="header">
More HTML Code
</div>
<?php
//More PHP Code ...
Works inside If's as well
PHP Code:
//PHP Code...
if(true)
{
?>
<div class="header">
More HTML Code
</div>
<?php
}else{
?>
<div class="header">
A Different Header
</div>
<?php
}
//More PHP Code ...
-
Sep 20th, 2005, 05:10 AM
#3
Thread Starter
PowerPoster
Re: Writing HTML inside of PHP documents..
Yeah, but then if you need to add in a single line of php you end up with the little nested <? print "" ?> through out your HTML. I kinda see that solution as the other end of the problem, where the HTML is readable, but the PHP isn't..
-
Sep 20th, 2005, 05:32 AM
#4
Addicted Member
Re: Writing HTML inside of PHP documents..
You can print shorthand by doing <?="I want to print this text" ?> or <?=$myText ?>
-
Sep 20th, 2005, 05:32 AM
#5
Re: Writing HTML inside of PHP documents..
This is how I do it: https://www.vbforums.com/showthread.php?p=2168862
PHP is/was originaly intended to be a templating language. Which is why, by default you are always in HTML mode. It is never a good idea to output large chunks of HTML using print and echo statements. Two of the reasons, readability and escaping quotes you have already identified; but here are a couple more:
- When ever you use a string enclosed in double quotes, PHP needs to check the entire string for embedded variables. This gives the PHP interpreter unecessary processing and makes the script slower.
- Any kind of output in the middle of a script is a bad idea. You should always keep the output clearly separate from the process. Limit any logic in output to display logic and keep the process logic, such as accessing databases in the main body of the script.
-
Sep 20th, 2005, 05:34 AM
#6
Re: Writing HTML inside of PHP documents..
 Originally Posted by Pc_Madness
Yeah, but then if you need to add in a single line of php you end up with the little nested <? print "" ?> through out your HTML. I kinda see that solution as the other end of the problem, where the HTML is readable, but the PHP isn't.. 
PHP Code:
<?php // tmeplate example ?>
<p><?php echo($variable) ?></p>
It seems readable to me
-
Sep 20th, 2005, 06:38 AM
#7
Thread Starter
PowerPoster
Re: Writing HTML inside of PHP documents..
Perhaps if you were doing something like,
PHP Code:
<a href="http://myurl.php?parameter1=<?php print $variable1 ?>¶meter2=<?php print $variable2?>¶meter3=<?print $variable3?>">Link</a>
It kinda gets to the point where you could probably save your self the trouble and just print out the whole line instead. In my IDE (TextWrangler) all strings show as pink, while variables are shown in blue, so it stands out pretty clearly when I nest them with Print..
Any kind of output in the middle of a script is a bad idea. You should always keep the output clearly separate from the process. Limit any logic in output to display logic and keep the process logic, such as accessing databases in the main body of the script.
Hmmmm, thats a good point... does my code snippet above count as output or logic? As the only other way of doing it would be to use a variable to hold the string or something like that... :\ Working with databases has really forced me to processing and output, but I feel like I could improve it abit more...
Last edited by Pc_Madness; Sep 20th, 2005 at 06:49 AM.
Don't Rate my posts.
-
Sep 20th, 2005, 07:10 AM
#8
Re: Writing HTML inside of PHP documents..
How about this?
PHP Code:
<?php // comment ?>
<a href="http://myurl.php?<?php echo("parameter1={$variable1}&parameter2={$variable2}&parameter3={$variable3}") ?>">Link</a>
Using the curly braces around the embedded variables isn't required, but it makes them stand out more and makes their parsing quicker. Small lines like this are ok, that is why PHP allows you to embed variables in a string.
Hmmmm, thats a good point... does my code snippet above count as output or logic?  As the only other way of doing it would be to use a variable to hold the string or something like that... :\ Working with databases has really forced me to processing and output, but I feel like I could improve it abit more...
I've never seen any clearly defined rules as to what should be classified as output of processing. You need to think when you create your output, what if I wanted to change the backend database - if the answer to your question, is I would have to change some of the code in the output, then the answer is, that that code should be in the main part of the script.
When it comes to databases. Especially loading multiple rows into an HTML table, I load the rows first into an array and traverse the array in the output part of the script. Yes, it does mean you need to store a copy of the data, however you gain a big advantage in portability as it makes no difference whether the data came from a database, a text file or a giraffes bottom.
-
Sep 20th, 2005, 07:58 AM
#9
Re: Writing HTML inside of PHP documents..
As of late, I only use a template engine for output. Usually Smarty.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 7th, 2012, 08:02 AM
#10
New Member
Re: Writing HTML inside of PHP documents..
 Originally Posted by Pc_Madness
Hey guys, just wondering how you guys go about writing HTML inside a PHP document? I've been doing it all inside Prints, which means I have to escape the talking marks, so although its probably easier to work with, I find it less readable, and just generally impossible to get it all indented properly for when I view the source in a browser.
PHP Code:
print "<div id=\"loginwrapper\">\n";
print " <div id=\"CreateLoginDiv\">\n";
print " <div id=\"CreateLoginDivTitle\">Please Login:</div>\n";
print " <div id=\"CreateLoginDivContent\">\n";
if($bError) print "<center><div class=\"ErrorBox\"><h4>Error</h4>Invalid Username or Password. Please try again.</div></center>";
print " <form method=\"POST\" action=\"./login.php\">\n
<label>Username<input type=\"text\" name=\"username\" ";
What method do you guys use?
this was right code my dear 
<?php
print "<div id=\"loginwrapper\">\n";
print " <div id=\"CreateLoginDiv\">\n";
print " <div id=\"CreateLoginDivTitle\">Please Login:</div><br>\n";
print " <div id=\"CreateLoginDivContent\">\n";
print " <form method=\"POST\" action=\"./login.php\">\n
<label>first name: <input type=\"text\" name=\"firs_tname\" /></label><br> ";
print " <label>last name: <input type=\"text\" name=\"last_name\" /></label><br> ";
print " <label>password: <input type=\"password\" name=\"password\" /></label> ";
print " <label><input type=\"submit\" name=\"subm\" value=\"go for...\" /></label><br> ";
?>
-
May 7th, 2012, 08:39 AM
#11
Re: Writing HTML inside of PHP documents..
what the heck? Really? You dredged up a SIX year old thread, just to repost what the OP posted in the first place and go "yeah, that's right." .... did you not read the whole thread? Where the general consensus was to NOT do that?
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|