Avatar of tobece
tobece
 asked on

How to compare a function by its value ?!?

Hi,
I was a little inspired by WP (or perhaps not...) so I have some functions on my codes like this which easier me to call it without re-writing echo/print:
function do_yes() {
	echo 'YES';
}

Open in new window


It seems a great solution so far... until I need to do something like this :
if ( do_yes() == 'write-yes-here-or-whatever' ) { 
// Do something 
echo 'I want THIS';
} else {
// Do other thing 
echo ' - I always receive THIS';
}

// Output: YES - I always receive THIS
// Preferable Output: I want THIS

Open in new window


Which wont work :( since writing do_yes() anywhere will directly echo YES

Can someone help me so I can do a simple IF-ELSE to the above case without having to modify all my functions?
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Dave Baldwin

A Function should provide you with some advantage over directly coding an operation.  For your simple example, creating and calling such a simple function causes more overhead than simply writing "echo 'YES';" each time.  And most functions should 'return' a value.  Often it's some result that shows whether the function succeeded or not.
Member_2_248744

greetings,  you say something about doing several functions so you do not have to write echo, and maybe save some typing, but then you have your echo-out function do_yes() used in an IF comparison -
if ( do_yes() == 'write-yes-here-or-whatever' ) {

which I can not in any way see how that would be useful for you to do, as the specific function  do_yes() or  do_header() or  do_footer() will always echo out just one value, , , a fixed code in the function, and the if( )  test is ONLY meant to be used if there is a changing Variable to be tested as in
if ($changing == 'Special') {
where $changing can have Many different values.
Can you tell us what you want this code to help you do -
if ( do_yes() == 'write-yes-here-or-whatever' ) {
because you already KNOW that do_yes() always echos 'YES', , , so the if test will always be either true or false and never change, making it useless.

what end effect (goal) are you trying to get with -
if ( do_yes() == 'write-yes-here-or-whatever' ) {

? ? ?
tobece

ASKER
Actually this is for a quite old development that neds to be tweaked an I think it will be better if I can only figure out how to apply such function in an IF comparison

And I think Ray provides somehow a better solution since I just need to do a slight modification to such function and then I can safely use them in an IF comparison :)

Here's what I do by the way:
function do_yes($echo=TRUE) {
    if ($echo) echo 'YES';
    return 'YES';
}

$x = do_yes(FALSE); // preparing do_yes() for coming IF comparison :)
if ( $x == 'YES' ) { echo 'MATCH'; } else { echo 'NOT MATCH'; }

Open in new window


Anyway, thanks all for sharing your thoughts...
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
tobece

ASKER
Really a nice solution for a little problem that I dont even have a clue for it :)
Ray Paseur

Thanks for the points and thanks for using EE, ~Ray