0

I was studying an example from mysqli connection and I am confused! the method connect_error returns string or null.so how can we use it in an if condition when is doesn't return true or false? example code:

<?php
$servername="localhost";
$username="root";
$password="";
$dbname="example";
$conn=new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error){
    die("connection is failed".$conn->connect_error);
      }
        ?>
mam1680
  • 11
  • 1
  • 1
    The condition doesn't have to be true or false. null is considered to be false, while any non-empty string is considered to be true. – Barmar Feb 11 '21 at 08:38
  • 1
    `null` is a "falsy" value and a non empty string is a "truthy" value – M. Eriksson Feb 11 '21 at 08:38
  • 2
    Anything inside an `if` condition will be converted to its boolean equivalent. Consult the [comparison table](https://www.php.net/manual/en/types.comparisons.php). – El_Vanja Feb 11 '21 at 08:38
  • 1
    Use is_null() method inside `if` condition and it returns bool value – Andrew Markhai Feb 11 '21 at 08:46

0 Answers0