0

how to detect parameter is not empty on laravel?

example i have params body like below :

{
  email : "haha@example.com"
}

when params email is exist's i want echo some string, example : "parameter email is exist's"

if params email not exits just echo "parameter is not exist's"

if(params['email'] != null) {
  print params email is exists's
}else{
  print params email doesn't exists's
}

how do that, i'm new on php / laravel?

4

4 回答 4

0

Laravel 面向对象的方式是:

if($email = array_get($params, 'email')){
   echo " params email exists";
}else{
   echo  "params email doesn't exist";
}
于 2015-11-09T12:44:32.663 回答
0

使用 php build in empty()函数检查数组键是否存在,根据数据类型不为空且不为空。

if (!empty($params['email']) {
  // not empty
} 

从手册:

如果 var 存在并且具有非空、非零值,则返回 FALSE。否则返回 TRUE。

以下内容被认为是空的:

  • ""(一个空字符串)
  • 0(0 为整数)
  • 0.0(0 作为浮点数)
  • "0" (0 作为字符串)
  • 无效的
  • 错误的
  • array() (一个空数组)
  • $var; (声明的变量,但没有值)
于 2015-11-09T12:55:34.663 回答
0

试试下面的代码

if( isset(params['email']) && !empty(params['email']) ) {
  echo " params email exists"
}else{
  echo  "params email doesn't exist"
}
于 2015-11-09T11:54:21.950 回答
0

如果您想知道数组键是否存在,请使用

if(array_key_exists('email',$params)){
//true
}

检查密钥是否存在并且它不是空的使用

   if(array_key_exists('email',$params)&&!empty($params['email']){
    //true
    }

请参阅 Robert 对 empty 函数视为空的内容的回答。

于 2015-11-09T19:18:30.047 回答