Php return
From w3cyberlearnings
Contents |
return
return calls within a function when the function end the execution, and finally it returns the function call.
Syntax
function myFunction(input) { if(input) { return true; } else { return false; } }
Example 1
<?php function myfunc() { return 100; } $getnum = myfunc(); echo $getnum; ?>
Output
100
Example 2: function to check input for array
- Return Array OR TRUE when the function input argument is an array.
- Return False, when the function input argument is not an array.
<?php function myArray($input) { if (is_array($input)) { return $input; } else { return false; } } // test 1 $myData = array('leader', 'manager', 'mother'); if (myArray($myData)) { echo 'It is an array <br/>'; } else { echo 'It is not an array<br/>'; } // test 2 $myName = "Michel Jackson"; if (myArray($myName)) { echo 'It is an array <br/>'; } else { echo 'It is not an array<br/>'; } ?>
Output
It is an array It is not an array
Example 3: check input for digit/integer
<?php $input = 'name'; function check_input($input) { if (is_int($input)) { return 1; } else { return false; } } if (check_input($input) == false) { echo 'it is not digit'; } ?>
Output
it is not digit
Example 4
<?php function check_input($input) { $r = false; if (is_int($input)) { $r = true; } else { $r = false; } return $r; } if (check_input(30)) { echo 'yes a number'; } else { echo 'not a number'; } ?>
Output
yes a number