English 中文(简体)
PHP功能内的线
原标题:Arrays inside a PHP function
  • 时间:2010-09-11 12:38:49
  •  标签:
  • php

I m试图从功能中获取阵列数据,但保留错误: 无定义可言:地位。 如何纠正这种情况的想法?

<?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[ code ];


?>
最佳回答

你们必须把你的职能所要求的回报价值储存在一个变量中。

$answer = state_values($state);

echo $answer["abbr"];

echo $answer["code"];
问题回答

你们不会把这一功能的价值退回到一个变数。

$status = state_values($state);

要使它恢复价值,你也必须给变数带来价值。

<代码>status=国家=价值(单位:美元); 这样做。

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 ];

或至少是把它放在一个阵容上

$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 ];

你们必须分配回报价值,例如:

$status = state_values($state);

http://www.un.org/Depts/DGACM/index_french.htm 该职能只能从inside获取。 http://www.em>outside。

你们没有从职能范围内进入阵列! 你们只是在那里界定。


以前,你可以通过在职能结束时归还阵列来改进你的职能,并仅重复一次(适用于DRY-principle(Don t Repeat You)):

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:

关于评论: 当然,有多种方式使这种功能变得更糟,例如,使用一个检查表:

//...
$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();
//..

但这不是问题;





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签