PHP function returns boolean

How can I return the returned function boolean?
I mean this in another programming language.

return true; // return false
+3
source share
6 answers

The question is not entirely clear, because you did not ask for something that you have already decided.

A function in PHP can be performed as follows.

function check($int) {
   if ($int == 3) {
      return true;
   } else {
      return false;
   }
}
if (check(3)) echo "Returned true!";
+9
source

Incredibly enough

return true;

eg.

function testIfABC($x) {
    if ($x === 'ABC') {
        return true;
    }
    return false;
}

although it would be easier to write as:

function testIfABC($x) {
    return ($x === 'ABC');
}

which will still return a boolean

+4
source
<?php

  function doSomething() {
    return true;
  }

, , :

  function check($var, $length) { 
    return (strlen($var)<=$length) && (isset($var));
  }

?>
+4

:

return ($data != "expected") or ($param == 17);
+1

PHP 5.5 boolval:

(PHP 5 >= 5.5.0, PHP 7) boolval —  

return boolval( $result );

boolean. , , :

To explicitly convert a value to boolean, use the (bool)or drives (boolean). However, in most cases, casting is not necessary, since the value will be automatically converted if the operator, function or control structure requires a logical argument  

return (bool) $result;
return (boolean) $result;
0
source

I understand the question, since in Visual C # you can define a function to return only bool. This does not apply to PHP. You can simply use the blabla ($ val) {return ($ val == "yes"? True: false) function; }. No support for "public bool ($ value) {return true; // false}"

-1
source

Source: https://habr.com/ru/post/1790694/


All Articles