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?

Member Avatar for deleted1234

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.

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

Didn't know that was updated, guess i'm still learning ;)

~G

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

Member Avatar for diafol

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.

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.

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

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 ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.