0

This simple foreach loop was working but now it's not and I can't see anything wrong. $FieldNames is an array of column names from the database table and $row is an array containing the values if it's an existing entry. It creates the variable names from the column names using variable variables, then populates them if there is an open record. If no record is open, then there are no values for $row. These values are ultimately used on a form but the form is not currently being populated when an existing entry needs to be updated.

The only change I know of is that this development PC recently updated to PHP 8 but otherwise the programming itself hasn't been touched. Any ideas?

// Create variable variables from table column names and populate on post or from existing entry
foreach ($FieldNames as $row=>$val) :
    $$val = (isset($_POST[$val])) ? safeData($_POST[$val]) : (isset($row[$val]) ? addslashes($row[$val]) : '');
endforeach;

Sample of $row:

(
    [ID] => 1
    [FirstName] => John
    [LastName] => Doe
    [UserLogin] => JohnD
    [UserPassword] => MyPassword
    [Email] => [email protected]
)

Sample of $FieldNames:

(
    [0] => ID
    [1] => FirstName
    [2] => LastName
    [3] => UserLogin
    [4] => UserPassword
    [5] => Email
)

$FieldNames should be providing the variable variables regardless of whether $row has any data and until PHP8 it was working.

I tried simplifying to this $$val = (isset($_POST[$val])) ? safeData($_POST[$val]) : safeData($row[$val]); and it gives values when the form has an entry but when empty, each missing variable gives an error.

1
  • It appears that PHP8 has broken a number of things on my sites to do with ternaries that are not nested. I am looking into it but so many I'm not sure where to proceed! Commented May 21, 2022 at 18:09

1 Answer 1

0

One of the new features of PHP8 is that nested ternaries must be bracket grouped inorder to prevent ambiguity. That means previously if you'd do:

$var = $condition ? $condition2 ? 'a' : 'b' : 'c';

you now have to do:

$var = $condition ? ($condition2 ? 'a' : 'b') : 'c';
Sign up to request clarification or add additional context in comments.

10 Comments

Great, thank you! Is it backward compatible with PHP 7 that my live server uses or do I need to wrap the old and the new into a conditional based on version? Anyway, based on what you said, it would make my ternaries something like this $$val = (isset($_POST[$val])) ? (safeData($_POST[$val]) : (isset($row[$val])) ? addslashes($row[$val]) : ''); but I must have something off in the syntax as the colon as it vgives an unexpected token ":" error.
I had the parenthesis off and should be like this $$val = (isset($_POST[$val])) ? safeData($_POST[$val]) : (isset($row[$val]) ? addslashes($row[$val]) : ''); but it still isn't giving values to the variables. No errors but no values either.
Then you can also do this instead: $$val = safeData(@$_POST[$val] ?? $row[$val])
@DonP I made a mistake. I should have suggested using || (logical or) instead of ?? (nullish coalescing). The reason is beacuse ?? will return the value on the right only if the left value is exactly equal to null. But since you're dealing with "falsey" values rather than null, the || is more appropriate. Replace the ?? with || and you should be fine.
@DonP Hold on. How did I not see this! You're trying to reference two different variables by the same name. You're using $row for both the loop key variable and the defaults variable. Give them different names. Rename the array that contains default values to $defaults, and the loop key to $key.
|

Your Answer

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