Page 1 of 1
Newbie Conditional Question
Posted: Mon Aug 19, 2002 10:49 am
by HUWUWA
Hi guys, can I do something like this:
<?php
If(...){
?>
regular HTML here
<?php
}
else{
?>
regular HTML here
<?php
}
?>
It would be very difficult for me to echo the HTML code because there is so much of it. Thanks.
Posted: Mon Aug 19, 2002 10:50 am
by JPlush76
yea I usually never echo html code
just open and close the php brackets when you wanna insert php code
Posted: Mon Aug 19, 2002 10:53 am
by HUWUWA
Thanks, you mean do it like I wrote above ? Did I do it correctly with the brackets ?
Posted: Mon Aug 19, 2002 10:54 am
by phpPete
Definitely!!
It's the recommended way, and it saves having to escape all your double quotes and such in you html attributes.
Posted: Mon Aug 19, 2002 11:00 am
by llimllib
I have to respectfully disagree; I echo all my HTML code. If you use
heredoc syntax for large pieces of code, it's easy to echo out, you don't need to escape anything. I make a function like printJobTable(); which references this large piece of code. This makes my php page look like pseudocode, very easy to follow:
printStartHeader();
printJavaScript();
printEndHeader();
printJobTable();
printCandidatePool();
printFooter();
This makes it very easy to drill down problems, and anyone who has to maintain my code in the future should find it very easy, I think.
Posted: Mon Aug 19, 2002 11:13 am
by HUWUWA
I'm gonna stick with phpPete's advice, he 'da Man !
Yo Pete, I'm on Long Island man, nice to see you are from New Jersey

Posted: Mon Aug 19, 2002 11:13 am
by nielsene
This is getting a little OT, but here's my take. No function of mine ever generates output; they return strings that the main script will echo (or do something else to). This comes in handy if you want to echo htmlspecialchars($page) to see the source to a paticular part that causing problems on some browsers that won't show source for a post'ed form.
Here's a skeleton of how I tend to do it
Code: Select all
$display = new HTMLDisplay($language);
$page = $display->beginPage($title);
$page .= $display->showNavBar($curPage);
if ($user->isLoggedIn())
$page .= $display->showSomething($user);
else
$page .= $display->showLoginForm();
$page .= $display->endpage();
echo $page;
HTMLDisplay for me tends to be a subclass of "UIBase" fo situations where I want an alternate format for the same data. HTMLDisplay also tends to have a myriad of helper functions, listToRadioButton, formatTable, and other tools for site templating.
Posted: Mon Aug 19, 2002 11:18 am
by llimllib
I like that, nielsene, but I try to avoid objects in my PHP because $this->function(); is half the speed of function();. Also, for debugging, I tend to just edit the function itself to do htmlspecialchars or print_r or whatever needs to be seen. I do like your method though, I'll try it when php 5 comes out.
Posted: Mon Aug 19, 2002 11:56 am
by twigletmac
I find it incredibly annoying to try and follow where a loop is going and what it encompasses if there's tons of <?php ?>'s about. However, I do believe it's very much down to personal preference but I just thought I'd throw in my £0.02.
Mac
Posted: Mon Aug 19, 2002 12:08 pm
by phpPete
One last OT point...
llimlib and nielsen;
Your way(s) are what I'm trying to elevate my level of coding to...moving from a procedural way of thinking to a more modular and ultimately OO way.
With new programmers, of which I count myself one, it's real easy in PHP to go procedural as it's generally quicker to code.
I posted a suggestion in the Suggestins forum
HERE
Posted: Mon Aug 19, 2002 12:21 pm
by llimllib
I wasn't going to say it over there, but mac alluded to it: coding above the "howto" level is very much a matter of personal style and experience. That sort of tutorial wouldn't make sense, because you've just got to develop your own personal style. The only way to do that is to make a ton of mistakes.