0

I'm trying to get the $keysList array out of the function, but seem be going wrong somewhere. I'm getting the error:

Passed Notice: Uninitialized string offset: 2 in C:\web\apache\htdocs\dev\case2.php on line 40 Notice: Uninitialized string offset: 1 in C:\web\apache\htdocs\dev\case2.php on line 40 Notice: Uninitialized string offset: 0 in C:\web\apache\htdocs\dev\case2.php on line 40 Output = a , a and a .

How to get this right?

<?php

$catHandle = "addCat";

function validCatKeys($catHandle,$keysList)
{

    switch($catHandle){

    case "addCat":

            $listCountryCode = 'US';
            $listUserName    = 'Norman';
            $listUserId      = '1';
            $keysList        = array($listCountryCode,$listUserName,$listUserId);
            return true;
        break;

    case "addSubCat":

        break;

    case "addElm":

        break;

    default:
       return false;
    }


}



if(validCatKeys($catHandle,$keysList = ''))
{

    echo 'Passed';
    list($a, $b, $c) = $keysList;
    echo "Output =  a $a, a $b and a $c.";

}else{echo 'Failed';
}



?>
3

3 Answers 3

1

Define the $keysList variable as passed by reference in your function definition:

function validCatKeys($catHandle,&$keysList)

(notice the &)

This will make any internal changes to the $keysList variable available outside the function.

Sign up to request clarification or add additional context in comments.

Comments

1
function validCatKeys($catHandle,$keysList)
{

    switch($catHandle){

    case "addCat":

            $listCountryCode = 'US';
            $listUserName    = 'Norman';
            $listUserId      = '1';
            $keysList        = array($listCountryCode,$listUserName,$listUserId);
            return $keysList;
        break;
    ....

Comments

1

please try below code its working Demo http://codepad.viper-7.com/2Eyym1 the array $keyList is local variable and has local scope so declare it global it will work.

<?php
    $catHandle = "addCat";

    function validCatKeys($catHandle,$keysList)
    {
        global $keysList;

        switch($catHandle){

        case "addCat":

                $listCountryCode = 'US';
                $listUserName    = 'Norman';
                $listUserId      = '1';
                $keysList        = array($listCountryCode,$listUserName,$listUserId);
                return true;
            break;

        case "addSubCat":

            break;

        case "addElm":

            break;

        default:
           return false;
        }


    }



    if(validCatKeys($catHandle,$keysList = array()))
    {

        echo 'Passed';
        list($a, $b, $c) = $keysList;
        echo "Output =  a $a, a $b and a $c.";

    }else{echo 'Failed';
    }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.