Page 1 of 1

Pass a variable, calculate and return a Boolean result

Posted: Sat Aug 29, 2009 2:36 pm
by zunebuggy
I know to pass a variable to a function, but I want to pass a variable to a function and have the function do a calculation based on that variable and return a boolean True or False value back to the script that called the function.

Can someone show an example of this.

Sorry, I'm sure this is very basic but I am new to PHP and could not find an example of this or any of my books.

Thank you.

Re: Pass a variable, calculate and return a Boolean result

Posted: Sat Aug 29, 2009 3:03 pm
by synical21
I would like to know this also... Sorry this isnt an answer, just showing interest :P

Re: Pass a variable, calculate and return a Boolean result

Posted: Sat Aug 29, 2009 3:05 pm
by Darhazer
If you know how to pass the variable, what is the problem?

Code: Select all

function test( $var ) 
{
    if ($var + 10 > 100)
        return true;
 
    return false;
}
 
var_dump( test(10) );
var_dump( test(90) );
var_dump( test(95) );

Re: Pass a variable, calculate and return a Boolean result

Posted: Sat Aug 29, 2009 3:12 pm
by AlanG

Code: Select all

<?php
function add_two($value) {
    if(is_numeric($value)) { // Checks if the value given is a numeric string
        $value += 2;
        return true;
    }
    else
        return false;
}
 
// Back to the global scope
$mynum = 4;
 
if(add_two(&$mynum))
    echo 'My number now holds the value: '.$mynum; // 6
else
    echo "Addition failed";
?>

Re: Pass a variable, calculate and return a Boolean result

Posted: Sat Aug 29, 2009 3:20 pm
by Darhazer
AlanG,
As a PHP 5 certified developer you should know that call-time pass-by reference is deprecated, and the proper way to write the code is:

Code: Select all

<?php
function add_two(&$value) {
    if(is_numeric($value)) { // Checks if the value given is a numeric string
        $value += 2;
        return true;
    }
    else
        return false;
}
 
// Back to the global scope
$mynum = 4;
 
if(add_two($mynum))
    echo 'My number now holds the value: '.$mynum; // 6
else
    echo "Addition failed";
?>
Not only because the possible notice, but in your way you can pass the variable by value to the function, and it will be unable to change the value, leading to wrong behavior.

Re: Pass a variable, calculate and return a Boolean result

Posted: Sat Aug 29, 2009 3:25 pm
by zunebuggy
Thank you everyone. Those examples worked great and I learned a lot from this. Darhazer, I have a question. Yours works great, but I noticed you didn't use an Else. Can you explain what is happening?

Thank you again.

Re: Pass a variable, calculate and return a Boolean result

Posted: Sat Aug 29, 2009 3:30 pm
by Darhazer
Since there is a 'return' in the if, the code after the if block will be executed only if the condition is not met, so there is just no need for else.

Else should be used if there is a code that will be executed after if/else block, for the both cases.
The function add_two will work in the same way with or without the else.

Re: Pass a variable, calculate and return a Boolean result

Posted: Sat Aug 29, 2009 3:36 pm
by AlanG
@Darhazer: Oops my bad. :oops: Wasn't thinking clearly. :drunk: Yeah, i put the else in just for show. It returns false if the value given is not a number.

Also, just to note that a function by default returns null.