简体   繁体   English

如何使用PHP将零值视为真?

[英]How to treat zero values as true using php?

Here is my sample code: 这是我的示例代码:

$issue_id = $_POST['issue_id'];
if(!empty($issue_id)){
  echo 'true';
}
else{
   echo 'false';
}

If I pass 0 to $_POST['issue_id'] by form submitting then it echo false. 如果我通过表单提交将0 to $_POST['issue_id']传递0 to $_POST['issue_id'] ,那么它将返回false。 Which I want is: Condition will be true if the following conditions are fulfilled: 1. true when I pass any value having 0. 2. false when I don't pass any value. 我想要的是:如果满足以下条件,条件将为真:1。当我传递任何值为0时为true.2。当我没有传递任何值时为false。 ie: $_POST['issue_id'] is undefined. 即: $_POST['issue_id']未定义。

I also tried this: 我也试过这个:

if(!isset($issue_id)){
  echo 'true';
}
else{
   echo 'false';
}


if(!empty($issue_id) || $issue==0){
  echo 'true';
}
else{
   echo 'false';
}

The last one is okay, meaning if I pass any value having ZERO then it will echo true. 最后一个是好的,这意味着如果我传递任何具有ZERO的值,那么它将回显真实。 But it will also echo true if I don't pass any value. 但如果我没有传递任何价值,它也会回应真实。 Any idea? 任何想法?

The last is okay, meaning if I pass any value having ZERO then it echo true. 最后一个是好的,这意味着如果我传递任何具有ZERO的值,那么它回显为真。 But it also echo true if I don't pass any value. 但如果我没有传递任何价值,它也会成立。 Any idea? 任何想法?

if (isset($_POST["issue_id"]) && $_POST["issue_id"] !== "") {
}

please notice I used !== not != . 请注意我用过!==!= this is why: 这就是为什么:

0 == "" // true
0 === "" // false

See more at http://php.net/manual/en/language.operators.comparison.php 有关更多信息,请访问http://php.net/manual/en/language.operators.comparison.php


also if you are expecting number you can use 如果你期待数字,你也可以使用

if (isset($_POST["issue_id"]) && is_numeric($_POST["issue_id"])) {
}

since is_numeric("") returns false 因为is_numeric("")返回false

http://php.net/manual/en/function.is-numeric.php http://php.net/manual/en/function.is-numeric.php


Alternatively if you expect number good option is filter_var 或者,如果你期望数字好选项是filter_var

if (isset($_POST["issue_id"]) {
   $issue_id = filter_var($_POST["issue_id"], FILTER_VALIDATE_INT);
   if ($issue_id !== false) { 
   }
}

since filter_var("", FILTER_VALIDATE_INT) will returns false and filter_var("0", FILTER_VALIDATE_INT) will return (int) 0 因为filter_var("", FILTER_VALIDATE_INT)将返回false并且filter_var("0", FILTER_VALIDATE_INT)将返回(int) 0

http://php.net/manual/en/function.filter-var.php http://php.net/manual/en/function.filter-var.php

When you get data from a form, remember: 从表单中获取数据时,请记住:

  • All text boxes, whether input or textarea will come as strings . 所有文本框,无论是input还是textarea都将作为字符串 That includes empty text boxes, and text boxes which contain numbers. 这包括空文本框和包含数字的文本框。
  • All selected buttons will have a value, but buttons which are not selected will not be present at all . 所有选定的按钮都有一个值,但未选中的按钮根本不存在 This includes radio buttons, check boxes and actual buttons. 这包括单选按钮,复选框和实际按钮。

This means that $_POST['issue_id'] will be the string '0' , which is actually truthy. 这意味着$_POST['issue_id']将是字符串'0' ,这实际上是真的。

If you need it to be an integer, use something like: $issue_id=intval($_POST['issue_id']) ; 如果你需要它是一个整数,请使用类似: $issue_id=intval($_POST['issue_id']) ;

if(isset($_POST['issue_id'])) {
  if($_POST['issue_id'] == 0) {
    echo "true";
  }
  else {
   echo "false";
  }
}

@Abdus Sattar Bhuiyan you can also full fill your two condition like below one: @Abdus Sattar Bhuiyan你也可以完全填满你的两个条件如下:

<?php
$_POST["issue_id"] = "0";
$issue_id = isset($_POST['issue_id']) ? (!empty($_POST['issue_id']) || $_POST['issue_id'] === 0 || $_POST['issue_id'] === "0")  ? true : false : false;
if($issue_id){
  echo 'true';
}
else{
   echo 'false';
}

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

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