Differeing PHP code behaviors

0

My PHP -- specifically an elseif statement -- does not behave the same way on AL2 as it does on my CentOS 7 dev server. Both systems are supposed to be running PHP v7.3. I will note the AL2 system does not have the php-tidy module installed, and I am having GREAT difficulty adding it; but, I do not know if that has anything to do with the problem. What would cause an if->elseif->else statement to perform one way on CentOS 7 and differently on AL2? BTW, it would appear that PHP on the AL2 system is dropping the last '->else' part. Also if I code the elseif part to be precisely $var === 1, it drops the 'elseif' part of the statement and does the else statement.

if($var > 1) {
  // echo HTML form stuff
}elseif($var == 1) {
  // echo HTML table header <th> code only (no form elements)
  // There is an '&' in the text to be echoed
}else{
  // echo different HTML table header code, again no form elements
  // There is a '/' in this text to be echoed
}

I'm not the best coder, but this code works PERFECTLY on CentOS 7, running PHP v7.3.33 (with the php-tidy module enabled.) Any guidance you all could provide would be greatly appreciated.

posta 2 anni fa177 visualizzazioni
2 Risposte
1
Risposta accettata

You'd need to debug value of $var on both setups and compare it. It's obvious that it will be different, and then based on result you can try to understand the root cause of difference. The simplest way to debug is to add var_dump($var); before that if-elseif-else statement.

con risposta 2 anni fa
  • This is the solution. I am doing an array_column on a column coming out of the database; but, the related table in the Prod database was one row shorter than the Dev database -- as was revealed to me by the var_dump($var); procedure. Suffice it to say, I am very glad my concerns about the PHP Runtime environment were unfounded.

1

IMO the root cause of your issue will be the use of the "Equal" comparator. PHP is a loosely typed language and using == (Equal) instead of === (Identical) allows PHP to automatically cast either value before making the comparison. This effect is compounded when a value is 0 or 1 because they will be interchanged with TRUE & FALSE so your initial intention to verify that the value of $var is "1" will become "is there a value in $var?"

You can read more about Comparison Operators on the PHP docs.

In a case where you are using two different Operating Systems, I would imagine you are dealing with different default PHP Runtime Configurations. To verify this you can export the current configuration from each instance using the phpinfo() function and perform a Diff.

BTW, don't leave the function accessible for longer than you need it as this is a security risk.

con risposta 2 anni fa
  • Thank you for that clarification about the Equal vs Identical, it is very interesting that == is just TRUE or FALSE, in that situation. I've tried to redo the ifelse statement, and if I set the variable to $var === '1', (with the single-quotes around the 1), I can get that part of the statement to work, but it seems that as soon as it decides it has determined which operation to do, it just breaks out of the if->ifelse->else statement completely, even though this statement is nested within a for statement which is still counting and incrementing a variable.

    I must mention this code works fine (even if it's just deciding TRUE or FALSE and not actually verifying the value of $var), on my CentOS dev server. I will also note that my dev server has 'Thread Safety' off, and the AL2 system has it turned on. Could it be Thread Safety that is breaking out of the ifelse statement? The Dev server is also using php-fpm and AL2 is not; some other seemingly unrelated php modules -- bcmath, mcrypt, tidy -- are running on Dev but not on AL2 as well. Why would enabling php-tidy, for example, have any effect on the way PHP 7.3.29 handles an ifelse statement? I'm pretty sure it would not have an effect.

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande