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.
Pass a variable, calculate and return a Boolean result
Moderator: General Moderators
Re: Pass a variable, calculate and return a Boolean result
I would like to know this also... Sorry this isnt an answer, just showing interest 
Re: Pass a variable, calculate and return a Boolean result
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
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
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:
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.
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";
?>Re: Pass a variable, calculate and return a Boolean result
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.
Thank you again.
Re: Pass a variable, calculate and return a Boolean result
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.
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
@Darhazer: Oops my bad.
Wasn't thinking clearly.
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.
Also, just to note that a function by default returns null.