什么样的if参数在PHP中求值? if(!a = foo()){//代码}

时间:2018-08-28 09:01:04

标签: php

我无法理解下面提到的代码。如果同时使用条件赋值和求反运算符。如果有人向我解释这一点,我将非常感谢您。

<?php
function foo() {
return mysqli_connect('localhost', 'username', 'password', 'dbname');
}

function start(){
    if(!$con=foo()){
    exit();
    }
return $con;
}
?>

1 个答案:

答案 0 :(得分:2)

在php中,我们不初始化变量之类的函数。我们可以通过这种方式定义功能

function foo() {
 $connection = mysqli_connect('localhost', 'username', 'password', 'dbname');
 return $connection; // it will return true or false on the basis of mysqli_connect() function
}
if(!$a =foo()) { //The negate in if tells us if it is not returning true 
//code goes here
}