/    Sign up×
Community /Pin to ProfileBookmark

bad habit or OK

me and my friend are havning a discussion if its ok or a bad habit to close php just after the if statement so you dont have to write echo? Give us your opinion.

if($foo){
?>
html code for the statement

<?php }?>
more html code..

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscMay 13.2008 — Opinion: I don't think it's bad habit. I think it's personal preference and either way is good. Personally I like the way you described much better...writing echo and having to escape " really sucks when you're echo-ing out html tags/attributes.
Copy linkTweet thisAlerts:
@bokehMay 13.2008 — Give us your opinion.[/QUOTE]Stop fantasizing over these insignificant trivialities and put the effort into something that is actually going to improve your scripts.
Copy linkTweet thisAlerts:
@SyCoMay 13.2008 — alexej please feel free to ask as many questions as you like here regardless of how noob you think they are or how trivial other more experienced coders might think they are. I always thought everyone was a noob at one time, but I could be wrong. Some people might well have been born with PHP in their veins. They will become frustrated that you don't 'feel' your way through the code. But don't that slow you down ?

You might have gathered from the responses, it's not all that important and usually reflects what happens within the conditional if statement. If you're echoing lots of HTML then you have to escape quotes and consider what else will effect the PHP. If you close the tag you don't have to. Maybe you're copying existing HTML from another script and closing the tags means you don't have to modify it and simply paste it in, saving time, which in a commercial environment, is money.

The PHP script is prepared for the browser before the HTML is sent so it's more important to consider how your script will execute. Inefficient SQL queries and crazy loops in loops are the usual things that slow down scripts.
Copy linkTweet thisAlerts:
@NogDogMay 14.2008 — Generally you should opt for whatever method makes the code easiest to read and therefore easiest to maintain during the lifespan of the application. If you get to a point where things start getting difficult to read no matter what you do, then it's probably time to start thinking about implementing some sort of templating system or otherwise separating the view from the rest of the application.
×

Success!

Help @alexej spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...