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?

Zack Tim
  • 66
  • 7

4 Answers4

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;
}
?>
John Doe
  • 1,401
  • 1
  • 3
  • 14
0

you can find answer

here

Either use as strings, or actual integers

  9 // Integers
 "09" // Strings
  • 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 – Zack Tim Apr 20 '21 at 04:16
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.

user1597430
  • 1,138
  • 1
  • 7
  • 14
  • Thank you, can you explain your code please because the output now is 2, where that came from? – Zack Tim Apr 20 '21 at 04:45
  • 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: https://www.php.net/manual/en/function.floor.php – user1597430 Apr 20 '21 at 04:48
  • 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. – user1597430 Apr 20 '21 at 04:50
  • Yes absolutely, I appreciate your time and your advice :) – Zack Tim Apr 20 '21 at 04:58
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.

Artier
  • 1,648
  • 2
  • 8
  • 22