anand0412 Posted July 27, 2022 Share Posted July 27, 2022 Hi all. I have no formal education in programming and learning everything through internet. Forgive me if this is too basic. I am creating a automated document from gravity forms in wordpress. This is the example of code that works. if ( is_array( $form_data['field'][77] ) AND count( $form_data['field'][77] ) == 1 ) { $v_bop1 = ' to ' . (implode(', ', $form_data['field'][77])) . '.'; } elseif ( is_array( $form_data['field'][77] ) AND count( $form_data['field'][77] ) > 1 ) { $v_bop1 = (implode(', ', $form_data['field'][77])); $v_bop1 = 'to ' . substr_replace($v_bop1, ' and', strrpos($v_bop1, ','), 1) . ' equally.'; } As you may have guessed, I am trying to get some data from gravity form and reformat it to suit my document. However, in order to work, I need to duplicate this code for each and every entry which seems inefficient. I tried to create a function using this code:- function GetBeneficiaries ( $benfield ) { if ( is_array( $form_data['field'][$benfield] ) AND count( $form_data['field'][benfield] ) == 1 ) { $beneficiaries = ' to ' . (implode(', ', $form_data['field'][benfield])) . '.'; } elseif ( is_array( $form_data['field'][benfield] ) AND count( $form_data['field'][benfield] ) > 1 ) { $beneficiaries = (implode(', ', $form_data['field'][benfield])); $beneficiaries = 'to ' . substr_replace($beneficiaries, ' and', strrpos($beneficiaries, ','), 1) . ' equally.'; echo $beneficiaries; } } I should then be able to call the function with GetBeneficiaries ( 77 ); However, my function is not returning any data / value. What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/315093-need-help-with-php-function-newbie-here/ Share on other sites More sharing options...
dodgeitorelse3 Posted July 27, 2022 Share Posted July 27, 2022 First thing I see is your benfield throughout your function needs the $ in front of it. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315093-need-help-with-php-function-newbie-here/#findComment-1598649 Share on other sites More sharing options...
mac_gyver Posted July 27, 2022 Share Posted July 27, 2022 11 minutes ago, anand0412 said: my function is not returning any data you would need to return $beneficiaries;, rather than to echo it, and then use the returned value in the calling code. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315093-need-help-with-php-function-newbie-here/#findComment-1598650 Share on other sites More sharing options...
anand0412 Posted July 27, 2022 Author Share Posted July 27, 2022 13 minutes ago, dodgeitorelse3 said: First thing I see is your benfield throughout your function needs the $ in front of it. Thank you for pointing it out. 6 minutes ago, mac_gyver said: you would need to return $beneficiaries;, rather than to echo it, and then use the returned value in the calling code. Thanks for the suggestion. I have implemented both your suggestions. For some reason I am still not getting any result. my new code is now function GetBeneficiaries ( $benfield ) { if ( is_array( $form_data['field'][$benfield] ) AND count( $form_data['field'][$benfield] ) == 1 ) { $beneficiaries = ' to ' . (implode(', ', $form_data['field'][$benfield])) . '.'; return $beneficiaries; } elseif ( is_array( $form_data['field'][$benfield] ) AND count( $form_data['field'][$benfield] ) > 1 ) { $beneficiaries = (implode(', ', $form_data['field'][$benfield])); $beneficiaries = 'to ' . substr_replace($beneficiaries, ' and', strrpos($beneficiaries, ','), 1) . ' equally.'; return $beneficiaries; } } echo GetBeneficiaries ( 77 ); I have been at this for 2 days now. I appreciate your help Quote Link to comment https://forums.phpfreaks.com/topic/315093-need-help-with-php-function-newbie-here/#findComment-1598651 Share on other sites More sharing options...
Solution mac_gyver Posted July 27, 2022 Solution Share Posted July 27, 2022 $form_data doesn't exist inside the function (there would be php errors.) you could actually just define the function to accept an array of data as the input and call the function with $form_data['field'][77] as the input parameter. this will allow the processing to be applied to any array of data, not hard-coded for $form_data['field']... 1 Quote Link to comment https://forums.phpfreaks.com/topic/315093-need-help-with-php-function-newbie-here/#findComment-1598652 Share on other sites More sharing options...
anand0412 Posted July 27, 2022 Author Share Posted July 27, 2022 13 minutes ago, mac_gyver said: $form_data doesn't exist inside the function (there would be php errors.) you could actually just define the function to accept an array of data as the input and call the function with $form_data['field'][77] as the input parameter. this will allow the processing to be applied to any array of data, not hard-coded for $form_data['field']... Awesome. That worked. Thank you very very much. I can finally sleep peacefully tonight. Quote Link to comment https://forums.phpfreaks.com/topic/315093-need-help-with-php-function-newbie-here/#findComment-1598653 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.