Newbie Conditional Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HUWUWA
Forum Commoner
Posts: 35
Joined: Sat Aug 17, 2002 7:24 pm
Location: Long Island, NY

Newbie Conditional Question

Post 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.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

yea I usually never echo html code

just open and close the php brackets when you wanna insert php code
HUWUWA
Forum Commoner
Posts: 35
Joined: Sat Aug 17, 2002 7:24 pm
Location: Long Island, NY

Post by HUWUWA »

Thanks, you mean do it like I wrote above ? Did I do it correctly with the brackets ?
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Definitely!!

It's the recommended way, and it saves having to escape all your double quotes and such in you html attributes.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
HUWUWA
Forum Commoner
Posts: 35
Joined: Sat Aug 17, 2002 7:24 pm
Location: Long Island, NY

Post 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 :D
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post 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
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
Post Reply