Jump to content

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
https://forums.phpfreaks.com/topic/304849-mind-numbing-issue-very-basic-code-bug/
Share on other sites

Your two versions of the code (posted and image) are different, so which do you expect us to try to debug?

 

The image code incorrectly uses "=" instead if "==" for the comparison, if that helps.

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>';
   }
}

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.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.