简体   繁体   中英

PHP Notice: Use of undefined constant idq - assumed 'idq'

Im getting this php notice , and my program works perfectly. here is my code :

    $data =  file_get_contents('php://input');
    $P = isset(json_decode($data)->E->EP->P) ? json_decode($data)->E->EP->P : '0';
    for ($i = 1; $i <= $P; $i++) {
        ${idq.$i} = isset(json_decode($data)->E->EP->{idq.$i}) ? json_decode($data)->E->EP->{idq.$i} : '0';
           }
echo ${idq.$i};

should i fix or suppress the error with handling?

Assuming you want your variables to be named $idq1 , $idq2 , $idq3 etc:

They're currently being considered as constants. If you want them in your variables, you need wrap your idq constant in quotes:

for ($i = 1; $i <= $P; $i++) {
    ${'idq'.$i} = isset(json_decode($data)->E->EP->{'idq'.$i}) ? json_decode($data)->E->EP->{'idq'.$i} : '0';
}

echo ${'idq'.$i};

But ternary statements make your code a bit ugly (at least, in this case). But, this looks a lot more cleaner, IMO:

$data =  file_get_contents('php://input');

if(isset(json_decode($data)->E->EP->P)) {
    $P = json_decode($data)->E->EP->P;
}
else {
    $P = '0';
}

for ($i = 1; $i <= $P; $i++) {

    if(isset(json_decode($data)->E->EP->{'idq'.$i}) ) {
        ${'idq'.$i} = json_decode($data)->E->EP->{'idq'.$i};
    }
    else {
        ${'idq'.$i} = '0';
    }

}

Also, to answer your final, question:

should i fix or suppress the error with handling?

Nope. Never. You should always find and fix the error instead of suppressing it.

始终修正您的错误,压制不是解决任何问题的方法,它只是一颗炸弹,随时待命……

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM