3

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. 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. i.e: $_POST['issue_id'] is undefined.

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. But it will also echo true if I don't pass any value. Any idea?

Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67
Abdus Sattar Bhuiyan
  • 3,016
  • 4
  • 38
  • 72
  • 1
    Be careful of `empty()`. Despite the name, it doesn’t check whether the variable is actually _empty_ — just whether it’s falsey. `null`, 0, false and an empty array, for example, are all considered “empty” in this sense. – Manngo Apr 13 '17 at 11:45

4 Answers4

6

The last is okay, meaning if I pass any value having ZERO then it echo true. 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


also if you are expecting number you can use

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

since is_numeric("") returns false

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


Alternatively if you expect number good option is 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

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

Peter
  • 16,453
  • 8
  • 51
  • 77
-1

When you get data from a form, remember:

  • All text boxes, whether input or textarea will come as strings. 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.

If you need it to be an integer, use something like: $issue_id=intval($_POST['issue_id']);

Manngo
  • 14,066
  • 10
  • 88
  • 110
  • @Peter The `@` operator exists for a reason. Judicious use of it would be to use it when you can handle the error in your stride, and where the absence of the value is otherwise harmless. In this case, a missing POST value shouldn’t be a deal breaker. I do agree that over using it is dangerous. – Manngo Apr 13 '17 at 08:37
  • 1
    there is many things that exists in PHP but it's bad practice to use them. Supressing errors no matter where is unarguably bad practice. In your case you are basically too lazy to use `isset` – Peter Apr 13 '17 at 08:42
  • Also **error messages exists for a reason**. There is countless number of articles about it https://www.sitepoint.com/why-suppressing-notices-is-wrong/ – Peter Apr 13 '17 at 08:44
-1
if(isset($_POST['issue_id'])) {
  if($_POST['issue_id'] == 0) {
    echo "true";
  }
  else {
   echo "false";
  }
}
Difster
  • 3,264
  • 2
  • 22
  • 32
  • it will echo true for empty string `""` – Peter Apr 13 '17 at 08:34
  • I really probably shouldn't be commenting this time of night. LOL However, if it's empty when the form is submitted, the issue_id variable is not going to exist. Unless something else in the code sets $_POST['issue_id'] after the form is posted, then my code works as expected. – Difster Apr 13 '17 at 08:36
  • @Difster In the absence of an explanation from @Peter, presuming the data comes from a text box, then `$_POST['issue_id']` will have the value of an empty string — all text boxes are sent, even if they are empty. However, your code would work better if you test `$_POST['issue_id'] === 0`, since an empty string and 0 are __equal__ but not __identical__; that is, one can be converted to the other. – Manngo Apr 13 '17 at 11:40
  • @Manngo `$_POST['issue_id'] === 0` would return false since $_POST values are always strings – Peter Apr 13 '17 at 12:17
  • @Peter Yes, you’re right. I should have said something more generic such as “test `$something===0` since etc”. Using `$_POST['issue_id']` was definitely a bad example. – Manngo Apr 13 '17 at 12:27
-1

@Abdus Sattar Bhuiyan you can also full fill your two condition like below one:

<?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';
}
lazyCoder
  • 2,544
  • 3
  • 22
  • 41