php自定义函数,用于检查值是否为空
我想简化这个检查值是否为空的PHP过程php自定义函数,用于检查值是否为空,php,function,Php,Function,我想简化这个检查值是否为空的PHP过程 <?php if(isset($_POST['criteria'])) { $value = $_POST['criteria']; if($value['recentsq'] == ""){$recentsq = null;}else{$recentsq = $value['recentsq'];} if($value['custname'] == ""){$custname = null;}else{$cus
<?php
if(isset($_POST['criteria']))
{
$value = $_POST['criteria'];
if($value['recentsq'] == ""){$recentsq = null;}else{$recentsq = $value['recentsq'];}
if($value['custname'] == ""){$custname = null;}else{$custname = $value['custname'];}
if($value['address'] == ""){$address = null;}else{$address = $value['address'];}
}
?>
如果该值为空,则变量将设置为null。如果不是,则将变量设置为该值
在本例中,我成功地将一个名为criteria的jquery对象发布到PHP进行处理。然后我将变量设置为对象中的值。我使用IF/ELSE语句检查对象值是否为空
<?php
if(isset($_POST['criteria']))
{
$value = $_POST['criteria'];
if($value['recentsq'] == ""){$recentsq = null;}else{$recentsq = $value['recentsq'];}
if($value['custname'] == ""){$custname = null;}else{$custname = $value['custname'];}
if($value['address'] == ""){$address = null;}else{$address = $value['address'];}
}
?>
我有很多这样的值需要检查和设置
我的问题是如何通过使用自定义函数简化此过程?您只需创建如下方法:
function customfunction($valueString){
return ($valueString != '' ? $valueString : null);
}
在上面的方法中,我使用三元运算符
在您的示例中,您可以像这样调用此方法:
$recentsq = customfunction($value['recentsq']);
$custname = customfunction($value['custname']);
$address = customfunction($value['address']);
未来访客的附加点:
,如果您使用的是PHP 7:
空合并运算符??已添加为语法糖
对于需要结合使用三元数的常见情况
伊塞特。如果第一个操作数存在且不为空,则返回第一个操作数;
否则返回第二个操作数
等于:
您只需创建如下方法:
function customfunction($valueString){
return ($valueString != '' ? $valueString : null);
}
在上面的方法中,我使用三元运算符
在您的示例中,您可以像这样调用此方法:
$recentsq = customfunction($value['recentsq']);
$custname = customfunction($value['custname']);
$address = customfunction($value['address']);
未来访客的附加点:
,如果您使用的是PHP 7:
空合并运算符??已添加为语法糖
对于需要结合使用三元数的常见情况
伊塞特。如果第一个操作数存在且不为空,则返回第一个操作数;
否则返回第二个操作数
等于:
或者您可以直接使用PHP7中的
if(isset($_POST['criteria']))
{
$value = $_POST['criteria'];
$recentsq = $value['recentsq'] ?? null;
$custname = $value['custname'] ?? null;
$address = $value['address'] ?? null;
}
或者您可以直接使用PHP7中的
if(isset($_POST['criteria']))
{
$value = $_POST['criteria'];
$recentsq = $value['recentsq'] ?? null;
$custname = $value['custname'] ?? null;
$address = $value['address'] ?? null;
}
可以使用以下函数从数组中获取值并设置默认初始值(默认为null
function arrayValue($array, $key, $default = null)
{
return (array_key_exists($key, $array) && $array[$key] !== '') ? $array[$key] : $default;
}
用法:
$values = [
'name' => 'David',
'age' => 18,
'sex' => '',
];
$name = arrayValue($values, 'name');
$age = arrayValue($values, 'age');
$sex = arrayValue($values, 'sex', 'unknown');
$country = arrayValue($values, 'country', 'unknown');
print_r(
[
$name,
$age,
$sex,
$country,
]
);
输出:
Array
(
[0] => David
[1] => 18
[2] => unknown
[3] => unknown
)
就像@christophe说的,因为php7可以做
$name = $values['name'] ?? null;
$country = $values['country'] ?? 'unknown';
可以使用以下函数从数组中获取值并设置默认初始值(默认为null
function arrayValue($array, $key, $default = null)
{
return (array_key_exists($key, $array) && $array[$key] !== '') ? $array[$key] : $default;
}
用法:
$values = [
'name' => 'David',
'age' => 18,
'sex' => '',
];
$name = arrayValue($values, 'name');
$age = arrayValue($values, 'age');
$sex = arrayValue($values, 'sex', 'unknown');
$country = arrayValue($values, 'country', 'unknown');
print_r(
[
$name,
$age,
$sex,
$country,
]
);
输出:
Array
(
[0] => David
[1] => 18
[2] => unknown
[3] => unknown
)
就像@christophe说的,因为php7可以做
$name = $values['name'] ?? null;
$country = $values['country'] ?? 'unknown';
或者,您可以使用空函数:
<?php
$recentsq = empty($_POST['criteria']['recentsq']) ? null : $_POST['criteria']['recentsq'];
$custname= empty($_POST['criteria']['custname']) ? null : $_POST['criteria']['custname'];
$address= empty($_POST['criteria']['address']) ? null : $_POST['criteria']['address'];
?>
然后,您甚至不需要进行单独的isset检查。或者,您可以使用空函数:
<?php
$recentsq = empty($_POST['criteria']['recentsq']) ? null : $_POST['criteria']['recentsq'];
$custname= empty($_POST['criteria']['custname']) ? null : $_POST['criteria']['custname'];
$address= empty($_POST['criteria']['address']) ? null : $_POST['criteria']['address'];
?>
然后你甚至不需要进行单独的isset检查。@JohnBeasley:当然:我得到了一个500错误,我能够将它隔离到函数调用中:$recentsq=customfunction$value['recentsq'];有什么想法吗?@JohnBeasley:检查php错误报告,或者不知道你在使用什么framework@Dharman:这是一些附加信息,如果OP使用PHP7,那么他可以使用它的函数体customfunction$valueString{我正在使用5.6。21@JohnBeasley:确定:我得到一个500错误,我能够将其隔离到函数调用:$recentsq=customfunction$value['recentsq'];有什么想法吗?@JohnBeasley:检查php错误报告,或者不知道你在使用什么framework@Dharman:这是一些附加信息,如果OP使用PHP7,那么他可以使用它的函数体customfunction$valueString{我正在使用5.6.21现在你有一些解决方案,检查它们现在你有一些解决方案,检查它们