开发者

If not following correct procedure?

开发者 https://www.devze.com 2022-12-13 15:10 出处:网络
I\'m working on a Content Management System, and so far so good. I\'m trying to get the system to work with a maintenance mode system.

I'm working on a Content Management System, and so far so good.

I'm trying to get the system to work with a maintenance mode system. This is the code in the script to display the maintenance stuff:

if ($maintenance['value'] == "1") {?>
    <div id="content">
     <div id="errmsg">
      <?php echo $maintenance['notes']; ?>
     </div>
    </div>
<? } else {?>

   <h1><?php echo $title; ?></h1>
   <hr /><br />
   <div id="content">
    <?php echo $contents; 
     if (!$content) { 
      include    ('includes/error/404.php');}?>
   </div>               
<? } ?>

I can verify that the $maintenance['value'] variable is working as it should, but this portion o the script isn't working as it should. The value is currently set to 1, but it still displays the stuff in the else.

Any id开发者_Go百科eas?


You can try to use a different syntax. Instead of opening curly brackets, you can use

<?php if ($maintenance['value'] == "1") : ?>

    <!-- HTML STUFF HERE -->

<?php ELSE: ?>

    <!-- more html stuff here -->

<?php ENDIF; ?>

From my experience, the PHP parser can parse this better.

See also the PHP manual for the alternative syntax


I second Cassy's proposed notation. My remarks:

I'm not really sure how PHP really handles datatypes but I just checked this site and according to this, your expression should evaluate to true, even if $maintenance['value'] holds the integer 1 (you are comparing it to the string "1").

What is the other value, $maintenance['value'] can hold? "0"? If so, why not just assign TRUE and FALSE to the variable, to be on the save side? Anyway, maybe you can try just to write your expression like this:

<?php if ($maintenance['value']) : ?>

This evaluates to TRUE if $maintenance['value'] holds any of these values:

  • true
  • 1
  • any integer != 0
  • "1"
  • any string != "0"


It appears you have an extra } following your else statement.


How about reversing the approach?

if (!isset($maintenance['value'])) { ?>

// echo normal non-maintenance stuff

<?php } else { ?>

// display maintenance stuff

<?php

}

That way you know whether the maintenance variable is being set, or not. And hopefully, perhaps with a echo "<pre>" . print_r(get_defined_vars(),true) . "</pre>"; see why.

Also, just because I'm typically atrocious and amnesiac at first-runs through if/else statements, are you sure you're using a comparison, rather than assignment, operator in the live code?

Post edited in response to following comment from OP:

that might not work, the value in the database is either 1 or 0. I should have made this clearer in OP.

If you know that $maintenance['value'] is boolean, 1 or 0, then why not use that knowledge and make:

if ($maintenance['value'] == "0") { ?>

// echo normal non-maintenance stuff

<?php } elseif ($maintenance['value'] == "1") {

// display maintenance stuff

<?php } else { 

echo "<pre>" . print_r(get_defined_vars(),true) . "</pre>";

}

Then you know exactly what's happening. Though not, of course, necessarily why. Which is probably the point of your question.


As other people have said, you're trying to compare between an int and a string. You can change everything to either using "1" or 1, or "0" or 0. An alternative and better method is to use the type-specific compare operators such as === or !== to match both value and type; and use the boolean terms true and false to make it painfully obvious to both yourself and the interpreter what you're trying to do.

You could also try using define("MAINTENANCE", true); for example, to make sure the value doesn't change halfway through the script. Though then again, this could make it more difficult to change the settings server-side unless you write new values to the file. Though then there might be issues with you writing while the server is trying to read the file for another client...

0

精彩评论

暂无评论...
验证码 换一张
取 消