Jump to content

Question about return true/false in functions


MaranV

Recommended Posts

I wrote this code as an assignment for php class, only after I was done and concluded it worked, I saw a comment on the assignment "returns true or false" so I added a true & a false to test, remaining clueless why this is necessary. I would appreciate it if someone could answer the questions i put in the comments :)

$hypo = $_POST["hypo"];	$mndsalaris = $_POST["mndsalaris"];   $mijnsom = hypotheekchecker( $hypo, $mndsalaris );  print "$mijnsom <br>";  $hypomax = maxhypo( $hypo, $mndsalaris );  print "$hypomax";function hypotheekchecker( $lening, $maandsalaris ) {$BenodigdeRente = $lening * 0.057;$BenodigdMsal = $maandsalaris * 3; if ($BenodigdeRente <= $BenodigdMsal) {echo " Gefelicflapstaart, u krijgt de hypotheek van ". bedrag_met_opmaak($lening);return true; //1. what exactly does this do besides add a 1 after the above echo is displayed ?   my function works the same without this line, excluding the 1 (probaly a boolean for true or something??)}    else {    echo "Helaas u verdient te weinig en u krijgt de hypotheek van ". bedrag_met_opmaak($lening) ." niet";  return false; // same thing, except this doesn`t add anything after the above echo  } }function maxhypo ($lening, $maandsalaris) {$BenodigdMsal = $maandsalaris * 3; $maxbedrag = $maandsalaris * 3 * 17.54385965;echo "De maximale hypotheek die u mag afsluiten bedraagt " . bedrag_met_opmaak($maxbedrag);} function bedrag_met_opmaak( $invoer_bedrag ) {  $antwoord =  "€ " . number_format($invoer_bedrag, 2);  return $antwoord;}

Edited by MaranV
Link to comment
Share on other sites

It is very unusual to make a function that actually prints out something. You make it return a value and then use the value instead. Like this:

function test($num) {    if($num > 5) {	    return true;    } else {	    return false;    }}$n = isset($_GET['num']) ? $_GET['num'] : 0;if(test($n)) {    echo "{$n} is greater than 5";} else {    echo "{$n} is less than or equal to 5";}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...