简体   繁体   English

如何存储isset条件php以避免重复

[英]how to store isset condition php to avoid repetition

my code: 我的代码:

if (isset($dayMarks[$res['resId']][$dDate])) {
     $var=$dayMarks[$res['resId']][$dDate];
     echo $var;
}

note that the isset condition is identical to the value assigned to $var, which creates a pretty ugly code. 请注意,isset条件与分配给$ var的值相同,这将创建一个难看的代码。

How is it possible to assign the condition to $var without repeating it? 如何将条件赋给$ var而又不重复呢?

(in javascript, I'd write if (var=$dayMarks[$re...) ) (在javascript中, if (var=$dayMarks[$re...)

This is a common problem in PHP where including files can create uncertainty about variables. 这是PHP中的常见问题,其中包含文件可能会导致变量不确定。

There are a two approaches that work well for me. 有两种方法对我有效。

Default Assignment 默认分配

With default assignment the $var variable will be given a default value when the key doesn't exist. 使用默认赋值时,键不存在时,会将$ var变量赋予默认值。

$var = isset($dayMarks[$res['resId']][$dDate]) ? $ var = isset($ dayMarks [$ res ['resId']] [$ dDate])吗? $dayMarks[$res['resId']][$dDate] : false; $ dayMarks [$ res ['resId']] [$ dDate]:false;

After this code can assume that $var will always contain a valid value. 此代码之后可以假定$ var将始终包含有效值。

Default Merger 默认合并

My preferred method is to always declare a default array that contains all the required values, and their defaults. 我的首选方法是始终声明一个包含所有必需值及其默认值的默认数组。 Using the False value to mark any keys that might be missing a value (assuming that key holds another value type besides boolean). 使用False值标记可能缺少某个值的任何键(假定该键除了布尔值外还具有其他值类型)。

$default = array(
    'date'=>false,
    'name'=>'John Doe'
);

$dayMarks[$res['resId']] = array_merge($default, $dayMarks[$res['resId']]);

This will ensure that the required keys for that variable exist, and hold at least a default value. 这将确保该变量所需的键存在,并至少保留一个默认值。

You can now test if the date exists. 现在,您可以测试日期是否存在。

if($dayMarks[$res['resId']]['date'] !== false)
{
     // has a date value
}

While this might not work exactly for your array. 虽然这可能不适用于您的阵列。 Since it looks like it's a table structure. 由于它看起来像是一个表结构。 There is a benefit to switching to named key/value pairs. 切换到命名键/值对是有好处的。 As this allows you to easily assign default values to that array. 这样一来,您可以轻松地将默认值分配给该数组。

EDIT: 编辑:

The actual question was if it was possible to reproduce the JavaScript code. 实际的问题是是否可以重现JavaScript代码。

if (var=$dayMarks[$re...) 如果(var = $ dayMarks [$ re ...)

Yes, this can be done by using a helper function. 是的,这可以通过使用辅助功能来完成。

NOTE: This trick should only be used on non-boolean types. 注意:此技巧只能用于非布尔类型。

 function _isset($arr,$key)
 {
    return isset($arr[$key]) ? $arr[$key] : false;
 }

 $a = array('zzzz'=>'hello');
 if(($b = _isset($a,'test')) !== false)
 {
    echo $b;
 }
 if(($c = _isset($a,'zzzz')) !== false)
 {
    echo $c;
 }

See above code here 看到上面的代码在这里

$isset = isset(...); //save the value
if ($isset) { .... }; // reuse the value
...
if ($isset) { ... }; // reuse it yet again

The only thing you can do is store $res['resId'][$dDate] . 您唯一可以做的就是存储$res['resId'][$dDate]

$var = $res['resId'][$dDate];
if( isset($dayMarks[$var]) ) {
    $var = $dayMarks[$var];
    echo $var;
}

If you only want to assign a variable processing simply, you can also write this as: 如果只想简单地分配变量处理,也可以这样写:

$var = $dayMarks[$res['resId']][$dDate]);
if (!isset($var)) unset($var);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM