开发者

Arrays inside a PHP function

I'm trying to access an arrays data from inside a function but keep getting the error: Undefined varaible: status. Any idea how to correct this?

<?php

$state = 'New York';

function state_values($state){
    switch ($state) {
        case 'New York':
            echo "State is $state";
            $status = array("abbr" => "NY", "code" => "03");
            return $status;
            break;
        case 'California':
            echo "State is $state";
            $status = array("abbr" => "CA", "code" => "06");
            return $status;
            break;
        case 'Washington':
            echo "State is $state";
            $status = array("abbr" => "WA", "code" => "05");
            return $status;
            break;
    }
}

# Call the function
state_values($state);


echo $status['abbr'];
echo $status['co开发者_C百科de'];


?>


You have to store the return value from your function call in a variable

$answer = state_values($state);

echo $answer["abbr"];

echo $answer["code"];


Since $status is being declared as a local variable in the function state_values, it's not accessible from outside the function unless you save the returned value to another variable.

Your code below should read as follows in order to capture the returned $status variable.

$status = state_values($state);

echo $status['abbr'];
echo $status['code'];

The PHP documentation has a very nice section on variable scope.


While it's not a very good way to handle it, you could do it using a global variable for $status like this:

$state = 'New York';
$status = array();

function state_values($state){
    global $status;

    switch ($state) {
        case 'New York':
            echo "State is $state";
            $status = array("abbr" => "NY", "code" => "03");
            break;
        case 'California':
            echo "State is $state";
            $status = array("abbr" => "CA", "code" => "06");
            break;
        case 'Washington':
            echo "State is $state";
            $status = array("abbr" => "WA", "code" => "05");
            break;
    }
}

# Call the function
state_values($state);

echo $status['abbr'];
echo $status['code'];

In the above, we declare $status outside the function, then specify inside the function that $status is a global variable. Any changes to $status that happen during the call to state_values change the global copy of the variable.

Like I said though, this makes for confusing & hard to maintain code so your initial approach is better. The above is more to illustrate how global variables work.


You aren't returning the value from the function into a variable.

$status = state_values($state);


Sine it returns value, you have to give value to a variable too.

$status = state_values($state); would do it.


I have to say that whole concept is wrong. It should be not a function or at least not in this form.

$state = 'New York';

function state_values($state){
  return dbgetrow("SELECT * FROM states WHERE name = %s",$state);
}
$status = state_values($state);
echo "State is $state, abbr is ".$status['abbr'];

or at least make it just array

$state_values = array(
   'New York' => array("abbr" => "NY", "code" => "03"),
   'California' => array("abbr" => "CA", "code" => "06"),
   'Washington' => array("abbr" => "WA", "code" => "05"),
);

echo $state_values[$state]['abbr'];


Well, you have to assign the return value, like:

$status = state_values($state);

The variable $status you declared in the function is only accessible from inside the function, not the outside code.

You are not accessing the array from inside the function! You only define it there.


Btw, you can improve your function by just returning the array at the end of the function and echoing just once (this applies to the DRY-principle (Don't Repeat Yourself)):

function state_values($state){
    echo "State is $state";

    // initialize the variable with an empty array
    $status = array();

    switch ($state) {
        case 'New York':
            $status = array("abbr" => "NY", "code" => "03");
            break;
        case 'California':
            $status = array("abbr" => "CA", "code" => "06");
            break;
        case 'Washington':
            $status = array("abbr" => "WA", "code" => "05");
            break;
    }
    return $status;
}

Update:

With regard to the comments: Of course there are various ways to make the function even nicer, e.g. using a lookup table:

//...
$states = array('New York' => array("NY","03"),
                'California' => array("CA","06"),
                'Washington' => array("WA","05"));

if(isset($states[$state])) {
    return array_combine(array('abbr', 'code'), $states[$state]);
}
return array();
//..

But that was not the question ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜