English 中文(简体)
界定违约阵列价值的最佳方式
原标题:Least verbose way of defining a default array value

是否有更清洁的办法来检查各种数值,以防止PHP通知? 我现在这样做:

$email      = (isset($_POST[ user_email ]))      ? $_POST[ user_email ]      :   ;
$first_name = (isset($_POST[ user_first_name ])) ? $_POST[ user_first_name ] :   ;
$last_name  = (isset($_POST[ user_last_name ]))  ? $_POST[ user_last_namel ] :   ;
$birthday   = (isset($_POST[ user_last_name ]))  ? $_POST[ user_last_namel ] :   ;

是否有办法做像贾瓦勒这样的事情,你刚才提供这样的违约情况?

user.email = response.email ||   ;

我不想压制通知,但这些丑恶的检查掩盖了我的法典。 页: 1

最佳回答

您可以发挥以下职能:

$email      = getPost( user_email );
$first_name = getPost( user_first_name );
$last_name  = getPost( user_last_name );
$birthday   = getPost( user_birthday );


function getPost($key, $default =   )
{
    if (isset($_POST[$key])) {
        return $_POST[$key];
    }
    return $default;
}

履行这一职能还使你能够更方便地进行额外疗养,例如,如果希望的话,trim(<>)。

你们也可以通过一个将退还的违约价值:

$flavor = getPost( flavor ,  vanilla ); // if $_POST[ flavor ] is not set,  vanilla  is returned
问题回答

如果你有一套属性,那么一个简明的选择是array_joint。 注:缺省首先:

 $post = array_merge(
      array("email" => "", "first_name" => "", "last_name" => "", "birthday" => ""),
      $_POST
 );

之后,他们才有机会接触:

 $email = $post["email"];   // it s assured there is some value

更细微的变数是:extract(,与。 但是,只有在这种结合中,才能防止进口任意变量:

 extract(array_merge($defaults, array_intersect_key(array_filter($_POST), $defaults)));

我至少看到了这样做的三种方式。

Adding array with defaults

$options = $_POST + $defaults;

$ 是个关联阵列,其编号为< 代码>_POST,但添加了从$defaults (除非在中为特定关键值的<>_POST_$_defaults上标明这一特定关键/价值,则该特定关键/价值与_$defaults被忽略,且该特定关键值在<_POST/<>上不予替换。

Merging with array of defaults

@mario(使用array_joint(<>):

$options = array_merge($defaults, $_POST);

Using helper functions...

...such as Kohana s Arr::get() helper:

/* ... */
public static function get($array, $key, $default = NULL)
{
    return isset($array[$key]) ? $array[$key] : $default;
}
/* ... */

后者具有一些易于替换违约价值的优势(根据违约情况为<代码>NUL)。 可以这样使用:

echo Arr::get($_POST,  user_email );

echo Arr::get($_POST,  user_email ,  N/A );
function assure(&$a, $default=null) {
  if (!isset($a)) $a=$default;
  return $a;
}

$email=assure($_POST[ user_email ],  );
$first_name=assure($_POST[ user_first_name ],  );

可在DefaultArray NSPL:

$post = defaultarray(  , $_POST);

$email = $post[ user_email ];
$first_name = $post[ user_first_name ];
$last_name = $post[ user_last_name ];
$birthday = $post[ user_birthday ];




相关问题
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 ...

热门标签