Jump to content

Recommended Posts

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>");
    }
}

 

Link to comment
https://forums.phpfreaks.com/topic/317150-code-stopped-working/
Share on other sites

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. 

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>");
    }
}

 

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>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.