why i get an default output 0 after the invalid inputs?

196 views Asked by At

My requirement is to set two values in an html form and pass those values into an PHP file where i will check wither these value is set or not set.If any one or two of the field is blank than it will show invalid input. And if the values are set (including 0) than it will do some action like as adding operation.But the problem is that if i set 0 it takes the value as empty value than shows invalid and also shows 0 after the invalid input. is this because add method is called.any explanation ? please anyone help me to understand it clearly and also release me from the confusion of 0 and empty check.

My code is here,

HTML:

<input type="number" name="num1"">
<input type="number" name="num2">
<input type="submit" name="add" value="+">

PHP:

<?php
class calculator_oop
{
    public $num1;
    public $num2;
    public $result;
   public function __construct($number1,$number2){
       if( ((empty($number1) || empty($number2))))  {
           echo "Invalid inputs ";
       }
       else{
           $this->num1 = $number1;
           $this->num2 = $number2;
       }
   }
   public function add(){
       return $this->result = $this->num1 + $this->num2;
   }
}
$ob = new calculator_oop($_POST['num1'],$_POST['num2']);
if($_POST['add'] =='+' ){
    echo $ob-> add();
}

When I keep the field blank, I just wanna know why 0 appears after invalid input when I let them blank.

output: Invalid input 0

2

There are 2 answers

1
Funk Forty Niner On BEST ANSWER

What's happening here is, 0 is considered as being empty (consult the reference on this below), but you've also (or may have) entered 0 in the input(s), to which in the eye of PHP and at the time of execution, is considered as being "not empty" at the same time, since the input(s) was/were not left "empty" which is sort of fighting for precedence/contradicting itself at the same time.

What you want/need to check is to see if it/they is/are numeric or not by using is_numeric() and using another conditional statement, rather than in one condition in the second statement.

Additionally, you could add an extra condition to check if the inputs are left empty, and adding required to each input, but don't rely on this solely.

if( (!isset($number1,$number2 )) 
    || !is_numeric($number1) 
    || !is_numeric($number2) )  {

        echo "Invalid input ";
       }

References:

NOTE:

Edit: After revisiting the question and during the time I was writing this, noticed you have posted your form.

Since you did not post your HTML form, this is the following that it was tested with:

<?php

// https://stackoverflow.com/q/41418885/

class calculator_oop
{
    public $num1;
    public $num2;
    public $result;
   public function __construct($number1,$number2){
//       if( (!isset($number1,$number2 )) || (empty($number1 || $number2)))  {

  if( (!isset($number1,$number2 )) || !is_numeric($number1) || !is_numeric($number2) )  {
           echo "Invalid input ";
       }
       else{
           $this->num1 = $number1;
           $this->num2 = $number2;
       }
   }
   public function add(){
       return $this->result = $this->num1 + $this->num2;
   }
}

if(isset($_POST['submit'])){

$ob = new calculator_oop($_POST['num1'],$_POST['num2']);
if($_POST['add'] =='+' ){
    echo $ob-> add();
    }

}

?>

<form action="" method="post">

Number 1: 
<input type="text" name="num1">
<br>
Number 2: 
<input type="text" name="num2">
<br>

<input type="text" name="add" value="+" readonly>

<br>

<input type="submit" name="submit" value="Submit">

</form>
0
Iteration On

In PHP, the following things are considered to be empty:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

if you want to test zero use :

$var == 0 or $var == "0"

you have to understand this :

<?php
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}
?>

Please read : http://php.net/manual/en/function.empty.php