Nugax Posted August 2, 2023 Share Posted August 2, 2023 I had the below function and it stopped working. I am assuming something changed in a new PHP Version since I wrote it. I am on Php 7.3.x now and this function returns no value. Can someone tell me where I went wrong! Thanks in advance. //Setup Text Array $CellProvidersMap = array ("ALLTEL"=> "sms.alltelwireless.com", "ATT"=> "@txt.att.net", "BOOSTMOBILE"=>"sms.myboostmobile.com", "VERIZON"=>"@vtext.com", "CRICKET"=> "mms.cricketwireless.net", "SPRINT"=>"messaging.sprintpcs.com", "STRAIGHTTALK-VERIZON"=>"@vtext.com", "VERIZON"=>"@vtext.com" ); $CellProviders = array ( "ALLTEL", "ATT", "BOOSTMOBILE", "CRICKET", "SPRINT", "STRAIGHTTALK-VERIZON", "VERIZON" ); //Display Cell Providers Routine function PrintCellProviders($default_value) { global $CellProviders; if (isset($default_value)) { print("<option selected>$default_value</option>"); } foreach ($CellProviders as $col_value) { print("<option>$col_value</option>"); } } Quote Link to comment https://forums.phpfreaks.com/topic/317150-code-stopped-working/ Share on other sites More sharing options...
Barand Posted August 2, 2023 Share Posted August 2, 2023 Your function does not contain a "return" statement to tell it to return a value Quote Link to comment https://forums.phpfreaks.com/topic/317150-code-stopped-working/#findComment-1610885 Share on other sites More sharing options...
Nugax Posted August 2, 2023 Author Share Posted August 2, 2023 Just now, Barand said: Your function does not contain a "return" statement to tell it to return a value Does it need a return value? It only prints html. Quote Link to comment https://forums.phpfreaks.com/topic/317150-code-stopped-working/#findComment-1610886 Share on other sites More sharing options...
Barand Posted August 2, 2023 Share Posted August 2, 2023 I don't know how you are trying to use it. All we have is the function without any context. You were complaining that it returns no value... 10 minutes ago, Nugax said: and this function returns no value. Quote Link to comment https://forums.phpfreaks.com/topic/317150-code-stopped-working/#findComment-1610887 Share on other sites More sharing options...
Nugax Posted August 2, 2023 Author Share Posted August 2, 2023 6 minutes ago, Barand said: I don't know how you are trying to use it. All we have is the function without any context. You were complaining that it returns no value... Ah... Actually, this function just prints. And from the calling php file, it stops executing with the error: Uncaught ArgumentCountError: Too few arguments to function. I think it is the way default_value is referenced in the call. It is called with no value, and prior PHP versions worked but something changed with 7. Quote Link to comment https://forums.phpfreaks.com/topic/317150-code-stopped-working/#findComment-1610888 Share on other sites More sharing options...
Nugax Posted August 2, 2023 Author Share Posted August 2, 2023 So the fix was this code... //Display Cell Providers Routine function PrintCellProviders($default_value = null) { global $CellProviders; if (isset($default_value)) { print("<option selected>$default_value</option>"); } foreach ($CellProviders as $col_value) { print("<option>$col_value</option>"); } } Quote Link to comment https://forums.phpfreaks.com/topic/317150-code-stopped-working/#findComment-1610889 Share on other sites More sharing options...
Barand Posted August 2, 2023 Share Posted August 2, 2023 I would write it like this... function PrintCellProviders($CellProviders, $default_value=null) { $opts = ""; foreach ($CellProviders as $col_value) { $sel = $col_value == $default_value ? 'selected' : ''; $opts .= "<option $sel>$col_value</option>"; } return $opts; // return the options } without using GLOBAL but passing the array instead. Then, in the HTML <select name='cellProvider'> <?= PrintCellPrividers($CellProviders) ?> <!-- output the returned options --> </select> Quote Link to comment https://forums.phpfreaks.com/topic/317150-code-stopped-working/#findComment-1610890 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.