Jump to content

mind numbing issue - very basic code bug?


baggs1981

Recommended Posts

Can anyone tell me what is wrong with this:

 

<?php

$anything = "yes";

if (isset($anything)) {

?>

<div>

<?php if ($anything == "yes") { ?>

blatext

<?php } ?>

</div>

<?php } ?>

 

I'm using Dreamweaver CC build 9314

 

The issue was in a 1000 line page and stripping everything out to get to the cause of error eventually leaves the above.

 

See attached pics error1.png is the above page, error2.png shows the further yellow code errors that show on the complete page.

 

If I delete the closing </div> entirely, it works.

If I change the first isset to any other type of if rule, it works.

If I add an extra </div> after blatext, it works.

 

As mentioned above the problem is within a 1000 line page, on that page the issue is compounded and there are 9 of the code problem yellow </div>

 

I have spent hours trying to figure this out, I've sat and counted out the <div> </div> pairs, checked while sections seperately by pasting them into separate pages and the above code is the only bit that has an issue.

 

Please help

post-205252-0-25097600-1504435351_thumb.png

post-205252-0-96514000-1504435406_thumb.png

Link to comment
Share on other sites

And - please - learn how to use the echo command and cease switching in and out of php mode.  That is 'mind-numbing' code!!!

<?php
$anything = "yes";
if (isset($anything))
{
   echo '<div>';
   if ($anything == "yes")
   { 
      echo 'blatext';
   }
   echo '</div>';
   }
}

Link to comment
Share on other sites

HTML fragments scattered all over the application code are even worse.

 

As far as old school PHP templating goes (which is inherently messy), switchting between the modes is fine. That's why they exist. You should use the verbose syntax for control structures, though.

<?php

$age = 44;

?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Title</title>
    </head>
    <body>
        <div>
            <?php if ($age >= 21): ?>
                Welcome to the site.
            <?php else: ?>
                You're too young to visit this site.
            <?php endif; ?>
        </div>
    </body>
</html>

If you want to do it properly, use a template engine like Twig. This not only gives you a much better syntax and plenty of useful features. It also prevents a large amount of security problems which PHP code notoriously suffers from.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.