Pass a variable, calculate and return a Boolean result

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zunebuggy
Forum Commoner
Posts: 41
Joined: Wed Aug 27, 2008 1:22 pm

Pass a variable, calculate and return a Boolean result

Post 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.
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

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

Post by synical21 »

I would like to know this also... Sorry this isnt an answer, just showing interest :P
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post 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) );
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

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

Post 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";
?>
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post 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.
zunebuggy
Forum Commoner
Posts: 41
Joined: Wed Aug 27, 2008 1:22 pm

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

Post 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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post 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.
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

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

Post 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.
Post Reply