Jump to content

Logic fails


Go to solution Solved by LDMartin1959,

Recommended Posts

I have the following code:

 

<?php if(($time_now<$str) && (($post->register=="0") || ($close_reg<$time_now))) {
                 echo $online_reg_closed_message;
                 } else if(($str<$time_now) && (($row->register=="0") || ($auto_close_reg<$time_now))) {
                 echo $reg_closed_message;
                 } else {
                 echo $final_message;
                 } ?>

 

I checked the values of the variables and they come back as this (shortened to significant values only for clarity:

 

$auto_close_reg

84340

 

$time_now

70164 (at the time of testing, at any rate)

 

$str

66400

 

$close_reg

16000

 

$post->register

0

 

Now, if the logic is functioning correctly I should (at the time of testing) be seeing the message stored in$reg_closed_message displayed. I'm not. I'm seeing $final_message.

 

It's probably something obvious but I can't see it. What did I do wrong here?

 

Thank you.

Edited by LDMartin1959
Link to comment
https://forums.phpfreaks.com/topic/284562-logic-fails/
Share on other sites

$time_now IS NOT less than $str.

 

Correct, which means the second if statement should be true (at least, if I did my logic correctly):

 

IF $str is less than $time_now [true] AND (($row->register=="0") [it is not] OR ($auto_close_reg is less than $time_now) [it is, thus making the OR clause true]) then echo $reg_closed_message

Edited by LDMartin1959
Link to comment
https://forums.phpfreaks.com/topic/284562-logic-fails/#findComment-1461400
Share on other sites

Odd.  I created the following test script:

  $time_now = 70000;
  $str = 66000;
  $close_reg = 16000;
  
  class testPost
  {
    public $register;
  }
  
  class testRow
  {
    public $register;
  }
  
  $post = new testPost;
  $post->register = "blah";
  
  $row = new testRow;
  $row->register = "0";
  
   if(($time_now < $str) && (($post->register == "0") || ($close_reg < $time_now))) {
      echo "online_reg_closed_message";
   } else if(($str < $time_now) && (($row->register == "0") || ($auto_close_reg < $time_now))) {
      echo "reg_closed_message";
   } else {
      echo "final_message";
   }

And it echoes "reg_closed_message" for me.  My only conclusion is that your values aren't what you think they are at the moment of the conditonals.

 

EDIT: Assigning a non-zero string to $row->register results in the same - "reg_closed_message" being echoed.

Edited by KevinM1
Link to comment
https://forums.phpfreaks.com/topic/284562-logic-fails/#findComment-1461403
Share on other sites

Odd.  I created the following test script:

  $time_now = 70000;
  $str = 66000;
  $close_reg = 16000;
  
  class testPost
  {
    public $register;
  }
  
  class testRow
  {
    public $register;
  }
  
  $post = new testPost;
  $post->register = "blah";
  
  $row = new testRow;
  $row->register = "0";
  
   if(($time_now < $str) && (($post->register == "0") || ($close_reg < $time_now))) {
      echo "online_reg_closed_message";
   } else if(($str < $time_now) && (($row->register == "0") || ($auto_close_reg < $time_now))) {
      echo "reg_closed_message";
   } else {
      echo "final_message";
   }

And it echoes "reg_closed_message" for me.  My only conclusion is that your values aren't what you think they are at the moment of the conditonals.

 

I echoed the values to the screen and those are the values it gave back (edited, as I said). The echo code is:

 

     echo "Start Time: " . $str . "<br />";
     echo "Online Reg Close: " . $close_reg . "<br />";
     echo "AutoClose: " . $auto_close_reg . "<br />";
     echo "Current Time: " . $time_now . "<br />";
     echo "Post register: " . ($post->register) . "<br />";

The returned values are (as of the time of testing):

 

Start Time: 1386266400
Online Reg Close: 1386201600
AutoClose: 1386277140
Current Time: 1386270164
Post register: 0
 
Unless my eyes are deceiving me (and I'll grant that's a possibility), these values still fulfill the second if statement as true.
Link to comment
https://forums.phpfreaks.com/topic/284562-logic-fails/#findComment-1461406
Share on other sites

  • Solution

Your second condition is using $row->register, not $post->register. Not sure if that is intentional or not, but guessing not since you never mention $row->register anywhere else.

No, wasn't intentional (although in terms of the logic result, it shouldn't make any difference as far as I can tell since the full statement still returns true). I am working on adding this logic to several WordPress templates on this site and apparently different people coded not only different pages but different portions of the pages and there are a LOT of inconsistencies in how things are done from page to page, and section to section. So that was my error for not getting all the changes made in that location. But I'm still not seeing any change in behaviour even with the correction. Frankly, there are tons of issues with this site: on a totally unrelated issue, the site has two different contact form which submit information via the mail() function, but one of them doesn't work despite the fact that PHP returns true from the function (which should mean that PHP successfully submitted the data) on both, the fact that both are in the same directory and both files are set to the same Unix permissions levels, since the one works it shouldn't be a PHP.ini setting and the one that failed simply stopped working one day even though no one touched the file or (as far as I can tell) any of the supporting files. Ugggh. This is probably not the best site for someone with my lack of PHP experience to be "gaining experience" with.  :shrug:

Link to comment
https://forums.phpfreaks.com/topic/284562-logic-fails/#findComment-1461490
Share on other sites

It's better to use var_dump to check what the values of your variables are, incase there is any implicit conversion going on when echoing that is making them appear to match when they don't. It'll also help you detect things like extra spaces/characters at the start/end of a string.

 

Before your if add

var_dump($str , $time_now , $row->register , $auto_close_reg);
so you can see the true values of each of the variables in your condition.
Link to comment
https://forums.phpfreaks.com/topic/284562-logic-fails/#findComment-1461504
Share on other sites

It's better to use var_dump to check what the values of your variables are, incase there is any implicit conversion going on when echoing that is making them appear to match when they don't. It'll also help you detect things like extra spaces/characters at the start/end of a string.

 

Before your if add

var_dump($str , $time_now , $row->register , $auto_close_reg);
so you can see the true values of each of the variables in your condition.

 

Thanks for the tip.

Link to comment
https://forums.phpfreaks.com/topic/284562-logic-fails/#findComment-1461508
Share on other sites

It also doesn't help that you're using WordPress, which is a terrible brew of bad coding practices and hacks under the hood.  Its ubiquity is in direct opposition with its quality.

Well, I just got thrown into this after the mess was made. I had nothing to do with creating it!! :)

Link to comment
https://forums.phpfreaks.com/topic/284562-logic-fails/#findComment-1461509
Share on other sites

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.