Solved

How to compare a function by its value ?!?

Posted on 2013-11-19
6
153 Views
Last Modified: 2013-11-21
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?
0
Comment
Question by:tobece
6 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39660959
Here is the introductory tutorial for PHP.

Sorry you took on WordPress as a learning tool.  That and Dreamweaver are two of the worst possible ways to learn PHP.  They embody an evil code smell in almost every block of programminng!  

If you want some good learning resources for PHP, this article will help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

To learn what not to do, this article will give you a beginners list.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12293-AntiPHPatterns-and-AntiPHPractices.html

In the instant case, having a function that uses echo should probably be added to the list of antipractices.  You may already depend on this functionality, so you probably need a new function when you want to return a value to the calling scope.  Or you might experiment with this concept.

<?php
function do_yes($echo=TRUE) {
    if ($echo) echo 'YES';
    return 'YES';
}

// USAGE EXAMPLE: ECHOES THE STRING VALUE
do_yes(); 

// USAGE EXAMPLE: RETURNS THE STRING VALUE
$x = do_yes(FALSE);
var_dump($x);

Open in new window

HTH, ~Ray
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39661037
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.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 39661486
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' ) {

? ? ?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:tobece
ID: 39666836
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...
0
 

Author Closing Comment

by:tobece
ID: 39666840
Really a nice solution for a little problem that I dont even have a clue for it :)
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39666935
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question