Home › Toolset Professional Support › [Resolved] sizeof() passing null – PHP 8.1
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | - |
- | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | - |
Supporter timezone: Europe/London (GMT+00:00)
This topic contains 3 replies, has 2 voices.
Last updated by Nigel 1 year, 11 months ago.
Assisted by: Nigel.
Tell us what you are trying to do?
Since switching to PHP 8.1, my code no longer works.
I have several checkbox categories. These checkboxes should be counted within the categories and then added together. The code worked great the whole time. I have now switched to PHP 8.1 and now I get the following error message when submitting the form.
The error appears as soon as no checkboxes are checked in one checkbox category (e.g. wpcf-checkboxes-bauen-renovieren) and it should output the value "null".
---------------------
Warning: Undefined array key "wpcf-checkboxes-bauen-renovieren" in /homepages/15/d845764028/htdocs/clickandbuilds/FirmeninfoRegional/wp-content/toolset-customizations/preis-logoduo.php on line 18
=> $field2 = sizeof($_POST['wpcf-checkboxes-bauen-renovieren']);
Fatal error: Uncaught TypeError: sizeof(): Argument #1 ($value) must be of type Countable|array, null given in /homepages/15/d845764028/htdocs/clickandbuilds/FirmeninfoRegional/wp-content/toolset-customizations/preis-logoduo.php:18
---------------------
Here is the code that I'm using:
add_action('cred_save_data','func_connect_child_posts',15,2); function func_connect_child_posts($post_id,$form_data) { if (($form_data['id']==352) or ($form_data['id']==687)) { if(!empty($_POST['wpcf-checkboxes-auto-verkehr'])) { update_post_meta($post_id, 'wpcf-count-checkboxes-auto-verkehr', sizeof($_POST['wpcf-checkboxes-auto-verkehr'])); } } if (($form_data['id']==352) or ($form_data['id']==687)) { if(!empty($_POST['wpcf-checkboxes-bauen-renovieren'])) { update_post_meta($post_id, 'wpcf-count-checkboxes-bauen-renovieren', sizeof($_POST['wpcf-checkboxes-bauen-renovieren'])); } } if (($form_data['id']==352) or ($form_data['id']==687)) { $field1 = sizeof($_POST['wpcf-checkboxes-auto-verkehr']); $field2 = sizeof($_POST['wpcf-checkboxes-bauen-renovieren']); $sum = $field1 + $field2; update_post_meta($post_id, 'wpcf-count-checkboxes-gesamtzahl', $sum); } }
How do I have to rewrite the code so that the form works again in PHP 8. Hope you can help me.
Is there any documentation that you are following?
https://toolset.com/forums/topic/how-to-count-number-of-selected-chechboxes/
Is there a similar example that we can see?
No
What is the link to your site?
hidden link
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
PHP 8 is less forgiving than previous versions, and what might previously have produced a warning (that you may not have noticed) now produces an error (which you will).
Your code is somewhat odd.
It has 3 if blocks, but each with the same condition, so the 3 blocks could all be merged.
Also, in your current code the second if block first checks if there is a value for the field wpcf-checkboxes-bauen-renovieren before using it (and calculating the sizeof), but the 3rd if block doesn't check if the value exists before using it, and so when it doesn't exist—because no checkbox was checked in the form—then it breaks the subsequent code that tries to use it.
I think you can combine your code into something simpler, like
add_action('cred_save_data', 'func_connect_child_posts', 15, 2); function func_connect_child_posts($post_id, $form_data) { if (($form_data['id'] == 352) or ($form_data['id'] == 687)) { if (!empty($_POST['wpcf-checkboxes-auto-verkehr'])) { $field1 = sizeof($_POST['wpcf-checkboxes-auto-verkehr']); update_post_meta($post_id, 'wpcf-count-checkboxes-auto-verkehr', $field1); } if (!empty($_POST['wpcf-checkboxes-bauen-renovieren'])) { $field2 = sizeof($_POST['wpcf-checkboxes-bauen-renovieren']); update_post_meta($post_id, 'wpcf-count-checkboxes-bauen-renovieren', $field2); } if (isset($field1) && isset($field2)) { $sum = $field1 + $field2; update_post_meta($post_id, 'wpcf-count-checkboxes-gesamtzahl', $sum); } } }
Hey Nigel, thanks a lot for your help.
It also gives me the error message in another form as soon as no checkbox in the group (checkboxes-bauen-renovieren) is checked:
Warning: Undefined array key "wpcf-checkboxes-bauen-renovieren" in /homepages/15/d845764028/htdocs/clickandbuilds/FirmeninfoRegional/wp-content/toolset-customizations/preis-logoduo.php on line 18
=> $field2 = sizeof($_POST['wpcf-checkboxes-bauen-renovieren']);
Fatal error: Uncaught TypeError: sizeof(): Argument #1 ($value) must be of type Countable|array, null given in /homepages/15/d845764028/htdocs/clickandbuilds/FirmeninfoRegional/wp-content/toolset-customizations/preis-logoduo.php:18
Can this code be rewritten for PHP 8 as well?
add_action('cred_save_data','func_price_firbasicduo',15,2); function func_price_firbasicduo($post_id,$form_data) { // Preis Checkboxen + Firmeneintrag if (($form_data['id']==352) or ($form_data['id']==533) or ($form_data['id']==651) or ($form_data['id']==683) or ($form_data['id']==684) or ($form_data['id']==685) or ($form_data['id']==687)) { $field1 = sizeof($_POST['wpcf-checkboxes-auto-verkehr']); $field2 = sizeof($_POST['wpcf-checkboxes-bauen-renovieren']); $field3 = sizeof($_POST['wpcf-checkboxes-behoerden-verbaende']); $field4 = sizeof($_POST['wpcf-checkboxes-bildung-wissenschaft']); $field5 = sizeof($_POST['wpcf-checkboxes-computer-elektronik']); $field6 = sizeof($_POST['wpcf-checkboxes-dienstleistungen']); $field7 = sizeof($_POST['wpcf-checkboxes-einkaufen-bestellen']); $field8 = sizeof($_POST['wpcf-checkboxes-freizeit-reisen']); $field9 = sizeof($_POST['wpcf-checkboxes-geld-recht']); $field10 = sizeof($_POST['wpcf-checkboxes-gesundheit-wellness']); $field11 = sizeof($_POST['wpcf-checkboxes-hotel-gastronomie']); $field12 = sizeof($_POST['wpcf-checkboxes-veranstaltungen']); $field13 = sizeof($_POST['wpcf-checkboxes-natur-umwelt']); $field14 = sizeof($_POST['wpcf-checkboxes-unternehmensbedarf']); $field15 = sizeof($_POST['wpcf-checkboxes-werbung-medien']); $field16 = sizeof($_POST['wpcf-checkboxes-wohnen-einrichten']); $pricefirbasicduo = ($field1 + $field2 + $field3 + $field4 + $field5 + $field6 + $field7 + $field8 + $field9 + $field10 + $field11 + $field12 + $field13 + $field14 + $field15 + $field16 - 1)*2.00 + 20.00; $pricefirbasicduorounded = number_format(round($pricefirbasicduo, 2), 2); update_post_meta($post_id, 'wpcf-preis-firbasicduo-ohne-toplink', $pricefirbasicduorounded); }
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
This is caused by the same issue, referring to a variable (an array element) without first checking if it actually exists.
You could write a more elegant solution for your problem that looped over a list of field keys, but manually you would need to do something like this:
add_action('cred_save_data','func_price_firbasicduo',15,2); function func_price_firbasicduo($post_id,$form_data) { // Preis Checkboxen + Firmeneintrag if ( in_array( $form_data['id'], array(352,533,651,683,684,685,687) ) ) { $field1 = empty($_POST['wpcf-checkboxes-auto-verkehr']) ? 0 : sizeof($_POST['wpcf-checkboxes-auto-verkehr']); $field2 = empty($_POST['wpcf-checkboxes-bauen-renovieren']) ? 0 : sizeof($_POST['wpcf-checkboxes-bauen-renovieren']); $field3 = empty($_POST['wpcf-checkboxes-behoerden-verbaende']) ? 0 : sizeof($_POST['wpcf-checkboxes-behoerden-verbaende']); $field4 = empty($_POST['wpcf-checkboxes-bildung-wissenschaft']) ? 0 : sizeof($_POST['wpcf-checkboxes-bildung-wissenschaft']); $field5 = empty($_POST['wpcf-checkboxes-computer-elektronik']) ? 0 : sizeof($_POST['wpcf-checkboxes-computer-elektronik']); $field6 = empty($_POST['wpcf-checkboxes-dienstleistungen']) ? 0 : sizeof($_POST['wpcf-checkboxes-dienstleistungen']); $field7 = empty($_POST['wpcf-checkboxes-einkaufen-bestellen']) ? 0 : sizeof($_POST['wpcf-checkboxes-einkaufen-bestellen']); $field8 = empty($_POST['wpcf-checkboxes-freizeit-reisen']) ? 0 : sizeof($_POST['wpcf-checkboxes-freizeit-reisen']); $field9 = empty($_POST['wpcf-checkboxes-geld-recht']) ? 0 : sizeof($_POST['wpcf-checkboxes-geld-recht']); $field10 = empty($_POST['wpcf-checkboxes-gesundheit-wellness']) ? 0 : sizeof($_POST['wpcf-checkboxes-gesundheit-wellness']); $field11 = empty($_POST['wpcf-checkboxes-hotel-gastronomie']) ? 0 : sizeof($_POST['wpcf-checkboxes-hotel-gastronomie']); $field12 = empty($_POST['wpcf-checkboxes-veranstaltungen']) ? 0 : sizeof($_POST['wpcf-checkboxes-veranstaltungen']); $field13 = empty($_POST['wpcf-checkboxes-natur-umwelt']) ? 0 : sizeof($_POST['wpcf-checkboxes-natur-umwelt']); $field14 = empty($_POST['wpcf-checkboxes-unternehmensbedarf']) ? 0 : sizeof($_POST['wpcf-checkboxes-unternehmensbedarf']); $field15 = empty($_POST['wpcf-checkboxes-werbung-medien']) ? 0 : sizeof($_POST['wpcf-checkboxes-werbung-medien']); $field16 = empty($_POST['wpcf-checkboxes-wohnen-einrichten']) ? 0 : sizeof($_POST['wpcf-checkboxes-wohnen-einrichten']); $pricefirbasicduo = ($field1 + $field2 + $field3 + $field4 + $field5 + $field6 + $field7 + $field8 + $field9 + $field10 + $field11 + $field12 + $field13 + $field14 + $field15 + $field16 - 1)*2.00 + 20.00; $pricefirbasicduorounded = number_format(round($pricefirbasicduo, 2), 2); update_post_meta($post_id, 'wpcf-preis-firbasicduo-ohne-toplink', $pricefirbasicduorounded); } }