Why does this value return true?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Osiris Dragon
    New Member
    • Aug 2011
    • 10

    Why does this value return true?

    Hi All, when running this in php, it returns true.

    Which doesn't make sense as 800 is smaller than

    Code:
    	if(0124 >= 0800)
    		{
    			echo "this doesnt makes sense";	
    		}
    Not sure why this is the case. However getting rid of the 0 in front fixes it. But I need to keep it as I am comparing time. EG: 0124 is greater than 0800 (8am) etc.

    Can someone tells me why PHP behaves this way when you prefix your number with 0?

    Many thanks.
  • firexfighterx
    New Member
    • May 2012
    • 14

    #2
    If you cast them to an int you wont have this problem.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      the problem with the leading 0 is that it will cast the number to octal. i.e. 0124 is converted to 84 and 0800 to 0 (because 8 is not part of the octal system)

      EG: 0124 is greater than 0800 (8am) etc.
      01:24 AM is greater than 08:00 AM?

      if you want to compare dates, use PHP’s date formats (e.g. the UNIX Timestamp or the DateTime classes)

      Comment

      • Osiris Dragon
        New Member
        • Aug 2011
        • 10

        #4
        Cheers guys. Type casting it fixed it.

        @Dormilich, thanks for the explanation. Never thought hex was in the picture

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          along with dormilich answer you should also know that if you parse 0 leading number in javascript you will get Octal value

          Comment

          Working...