0

Is there any reason not to echo content like this:

<!--Some content up here-->
<?php if($variable === true) { ?>
    <span>Some Content</span>
    <img src="some_img.png" alt="img"/>
<?php ;} ?>
<!--Some more content down here-->

Or should I be directly echoing everything that is dynamic?

6
Contributors
9
Replies
10
Views
7 Years
Discussion Span
Last Post by cloud09
0

I believe using echo or closing your PHP tags and inserting HTML would work the same way with dynamic content. I prefer not using echo whenever there are tons of HTML tags that I need to open close with "";.

Additionally, you don't need to add a ';' anymore before closing your if condition with a '}'. That would cause a syntax error.

0

Line 2 - You have three equal operators (===), this is not allowed, use == to compare two variables

Line 5 - You dont need the ; in front of the }

You can use this method to show HTML, although it is better to use the heredoc syntax or just plain echo().

~G

0

I think cloud09 just asked a simple question whether it should write the HTML in echo or not, and you guys started pointing his errors :P

Yes, there is no difference you can use the both ways. But if you are using heavy HTML then it's better to don't ECHO that because you were not able to view the design view of your HTML code. Otherwise there is not any problem by using html in ECHO.

Thanks

0

I think cloud09 just asked a simple question whether it should write the HTML in echo or not, and you guys started pointing his errors :P

Yes, there is no difference you can use the both ways. But if you are using heavy HTML then it's better to don't ECHO that because you were not able to view the design view of your HTML code. Otherwise there is not any problem by using html in ECHO.

Thanks

With respect, no pointing out of errors. Just discussion on the use of '===' - could it/should it? Nobody had a go at the OP and Graphix pointed the guy in the right direction. So don't see your point.

Anyway, heredoc syntax great for loads of normal html within php tags. Everybody has their own way of doing things. Echo, echo, echo and echo again until you can't echo any more is one method - but it can be difficult to maintain and I wouldn't necessarily advise it.

A simple rule of thumb, especially if you're starting out and aren't creating enterprise solutions etc, is to print out html through php if php > html (or use heredoc) OR have echoes within html if html > php.

0

I've implemented the no echo method I posted above and just wanted to make sure that it was acceptable.

Also I've never received any warning or error from the ; before the }, I just assumed it would be the same as closing a statement.

And as for the '===' its to ensure that the value is boolean true, as any non-zero value can be recognized as true with '==' (Correct me if I'm wrong on this). One example is if you are testing to make sure a command worked. Certain commands might return 0, like floor; if you only test if failed with "==", the if statement would be true. If you test with '===', the test would only succeed if 'floor' failed.

0

CloudNine,
Errors & error messages (if any) will depend on changes to server php versions, and will come when/if your host updates the version of php on the server, always without advising anybody before making the change.
If you are aware of the possibility of error, and the likely cause(s), when/if the error occurs, the solution is already known, and you can remove that semicolon, or switch to heredoc syntax
file it, under JustInCase

incidentally some code is much messier than that
time constraints to 'get it working' and make it prettier later
later always comes much later (never) after someone paying the bills has asked for yet another urgent change
form follows function, or a good landing is any you can walk away from

dos v'danye

Edited by almostbob: n/a

0

Sorry, I didn't mean to make it seem like I wasn't going to fix my mistakes, of which I probably have many more. I was just stating that the ; had never produced any errors before, so I wasn't aware is was an issue.

Thanks all who replied ;)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.