1

I have a very simple php code that echos specific numbers based on the time, however it always point out that case 09 is the problem. Here is my code:

<?php

$hour = date('H');


switch ($hour) {
        //Midnight
    case 00:
        echo '1';
        // 3 AM
    case 03:
        echo '2';
        // 6 AM
    case 06:
        echo '3';
        // 9 AM    
    case 09:
        echo '4';
        // Mid-day    
    case 12:
        echo '5';
        // 3 PM    
    case 15:
        echo '6';
        // 6 PM    
    case 18:
        echo '7';
        // 9 PM    
    case 21:
        echo '8';
}

Error: PHP Parse error: Invalid numeric literal in line 17

λ php -version
PHP 7.4.16 (cli) (built: Mar  2 2021 14:06:13) ( NTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Is this a bug?

7
  • 1
    When you start a number with a leading zero, PHP interprets it as an octal notation. Commented Apr 20, 2021 at 4:09
  • Is there any workaround this? time is given as integer and when i put the case inside ".." it would consider it as a string.. Commented Apr 20, 2021 at 4:14
  • Also, instead of a giant switch/case consider to use an associative array. Commented Apr 20, 2021 at 4:14
  • No, time is given as a string. Commented Apr 20, 2021 at 4:17
  • It's 4AM currently and when using $hour = date('H'); it would output= 04. From my code it should echo number 2. Nothing is echoed Commented Apr 20, 2021 at 4:25

4 Answers 4

1

I hope you are fine. This error arise from the changes made to how integers, specifically octals, are handled in PHP7 (as oppsoed to PHP5).

You should use them either as strings, or actual integers.

Means your should use 1 instead of 01 or if you want to use 01 then use it like string for example: "01".

<?php
$hour = date('H');
switch ($hour) {
    //Midnight
    case "00":
        echo '1'; break;
        // 3 AM
    case "03":
        echo '2'; break;
        // 6 AM
    case "06":
        echo '3'; break;
        // 9 AM    
    case "09":
        echo '4'; break;
        // Mid-day    
    case "12":
        echo '5'; break;
        // 3 PM    
    case "15":
        echo '6'; break;
        // 6 PM    
    case "18":
        echo '7'; break;
        // 9 PM    
    case "21":
        echo '8'; break;
}
?>
Sign up to request clarification or add additional context in comments.

5 Comments

I'm doing good thank you, the error is gone however nothing is echo'ed
What is the value of hour ?
the output is 04
@ZackTim, you should read a little about switch/case, obviously you don't understand how it works. Also, breaks are missed.
... that's because you forgot to put breaks.
0

you can find answer

here

Either use as strings, or actual integers

  9 // Integers
 "09" // Strings

1 Comment

The problem here is that the Hour time from time function is given like this: 04, 05 ... so if I put it as a 9 it wouldn't match because 9 is not equal like 09, I tried "09" and for all other cases but I don't see the echo
0

OP, your code can be simplified to this:

echo floor($hour / 3) + 1;

Also, your initial code misses break; in the end of each case section and zeros must be truncated for each case condition (or wrap numbers as strings).

P.S. You can also use date('G') instead of date('H') to reduce problems that you create with leading zeros.

4 Comments

Thank you, can you explain your code please because the output now is 2, where that came from?
php.net has a great explanation for all core PHP functions. The code above has only one function, you can read it's description here: php.net/manual/en/function.floor.php
Also, I highly recommend you to read most of pages on php.net to understand basics of PHP and C-like programming, because your questions are not about any specific problem, they are about programming in general.
Yes absolutely, I appreciate your time and your advice :)
0
decimal     : [1-9][0-9]*(_[0-9]+)*|0

octal       : 0[0-7]+(_[0-7]+)*

from document regex for consider value as decimal and octal are given above

In this scenario with value is 09 not pass validation for octal or decimal as well. since it is given error, parse parameter to string is solving problem partially.

Note: In PHP any number starts from zero considered as octal but 8 and 9 will not used in octal number resulting throw this error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.