How can I have a boolean returning function?
I mean like this on other programming language.
return true; // return false
The question does not entirely make sense, because you have not asked something you've already solved.
The function in PHP can be done like this.
function check($int) {
if ($int == 3) {
return true;
} else {
return false;
}
}
if (check(3)) echo "Returned true!";
<?php
function doSomething() {
return true;
}
The function you need could look like this:
function check($var, $length) {
return (strlen($var)<=$length) && (isset($var));
}
?>
Since PHP 5.5 you can use the boolval function:
(PHP 5 >= 5.5.0, PHP 7) boolval — Get the boolean value of a variable
return boolval( $result );
Or just convert to boolean. But perhaps all this is not necessary:
To explicitly convert a value to boolean, use the
(bool)or(boolean)casts. However, in most cases the cast is unnecessary, since a value will be automatically converted if an operator, function or control structure requires a boolean argument.
return (bool) $result;
return (boolean) $result;