Parse Error: Invalid Numeric Literal

Parse error: Invalid numeric literal

This comes from the changes made to how integers, specifically octals, are handled in PHP7 (as oppsoed to PHP5).

From the documentation (from PHP7 migration)

Invalid octal literals

Previously, octal literals that contained invalid numbers were silently truncated (0128 was taken as 012). Now, an invalid octal literal will cause a parse error.

From the documentation of integers

Prior to PHP 7, if an invalid digit was given in an octal integer (i.e. 8 or 9), the rest of the number was ignored. Since PHP 7, a parse error is emitted.

Either use them as strings, or actual integers

$a = array(1, 8, 9, 12); // Integers
$a = array("00001", "00008", "00009", "00012"); // Strings
  • Example of PHP7 vs PHP5: https://3v4l.org/GRZF2
  • http://php.net/manual/en/language.types.integer.php
  • Manual: http://php.net/manual/en/migration70.incompatible.php

Issue with php parse error: Invalid numeric literal

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;
}
?>

parse error: Invalid numeric literal at line 1, column 8

Some hints:

  • Do not put everything in one line, make it readable instead.
  • Structure your code with functions.
  • Do error handling.
  • Use Bash's debugging functionality.
  • Do not build JSON with string concatenation, use JQ instead, because only JQ quotes JSON data correctly. A password may contain quoting characters.

An example:

set -eu
set -x

USER_EMAIL="user@domain.org"
USER_PASSWORD="password"
CLIENT_ID="id"
CLIENT_SECRET="secret"
DOMAIN_NAME="domain.org"

data()
{
local template='
{
"grant_type": "password",
"username": $username,
"password": $password,
"audience": "https://localhost:8443/my-composite-service",
"scope": "openid email test:read test:write",
"client_id": $client_id,
"client_secret": $client_secret
}'

if jq <<<null -c \
--arg username "${USER_EMAIL}" \
--arg password "${USER_PASSWORD}" \
--arg client_id "${CLIENT_ID}" \
--arg client_secret "${CLIENT_SECRET}" \
"$template"
then
return
else
printf "ERROR: Can not format request data." >&2
exit 1
fi
}

post()
{
if curl --request POST \
--url 'https://${DOMAIN_NAME}/getmy/token' \
--header 'content-type: application/json' \
--data "$1" \
-s
then
return
else
printf "ERROR: Can not send post request." >&2
exit 1
fi
}

token()
{
if jq -r .access_token
then
return
else
printf "ERROR: Can not parse JSON response." >&2
exit 1
fi
}

TOKEN="$(post "$(data)" | token)"

PHP: mktime() parse error invalid numeric literal

Just remove the leading zeros.

mktime(8, 0);

It's because PHP is interpreting 08 as an octal number, and 8 is out of range in octal (0-7).

Parse error: Invalid numeric literal in if block

An integer literal in PHP is in octal if it starts with a leading zero. Knock off the leading zero and you'll be able to go above seven!

From the manual:

To use octal notation, precede the number with a 0 (zero).

Also, you seem to be comparing strings with numbers. If $IDKood is an array of characters, then perhaps you should be doing a string comparison:

if ($IDKood[3] . $IDKood[4] == '09') {

..though as ishegg observes, as long as you knock the leading zero off, the implicit conversion from string to integer will work fine:

if ($IDKood[3] . $IDKood[4] == 9) {

Having said all that, if you're just converting a number to a month name, there may be better ways to do it.

 $dateObj   = DateTime::createFromFormat('!m', (int) ($IDKood[3] . $IDKood[4]));
$monthName = $dateObj->format('F');


Related Topics



Leave a reply



Submit