1

I have a function that can either be called by echo or load, in turn also 0 or 1 for echo or load. But, since adding the number variable to the function options, it no longer always works.

Can you please help me?

Here is my function:

// GET MAIN INFORMATION
function get_info ($KEY, $OUTPUT = 0) {

$KEY = "_" . strtoupper($KEY);
if ($OUTPUT == "echo" || $OUTPUT == 0):
    echo "echo " . constant($KEY);
elseif ($OUTPUT == "load" || $OUTPUT == 1):
    return "load " . constant($KEY);
endif;

}

now when I try to call the function like this:

echo 1;
get_info('scripts_url', 'echo');

echo "<br>" . 2;
get_info('scripts_url', 0);

echo "<br>" . 3;
echo get_info('scripts_url', 'load');

echo "<br>" . 4;
echo get_info('scripts_url', 1);

This is my output:

1echo scripts/ (This is correct!)
2echo scripts/ (This is correct!)
3echo scripts/ (This is not correct!)
4load scripts/ (This is correct!)

So there is an issue when I try to use the numbers. Can you see the error and help me correct it?

Thanks

Denver William
  • 464
  • 2
  • 11
  • use `===` instead of `==`. The non-numeric string `'load'` compares truthfully to integer `0` owing to PHP's type juggling rules, so the first condition will match any string that isn't numeric. – Michael Berkowski Dec 11 '13 at 17:03
  • possible duplicate of [Why is integer 0 equal to a string in PHP?](http://stackoverflow.com/questions/13970544/why-is-integer-0-equal-to-a-string-in-php) – Michael Berkowski Dec 11 '13 at 17:04
  • If you add that as an answer, i will accept it. Thank you! It works like a charm! – Denver William Dec 11 '13 at 17:05
  • @MichaelBerkowski, your answer is a duplicate, the question is not. Thanks for the help. – Denver William Dec 11 '13 at 17:07
  • The linked duplicate is a signpost to better & more complete answers on this same issue. I've posted below if you would like to close your question with an accept. – Michael Berkowski Dec 11 '13 at 17:13
  • Why don't you just return the value, then use `echo get_info()`? – Nic Wortel Dec 11 '13 at 17:22
  • @nic it is just the way I want to do it. The majority of times I use this function, I will be doing so for echo, however, there will be a few times i need it for the variable like in a function, and this way I can do so without having to write echo hundreds of times, and instead only need add `, 0` a few times. – Denver William Dec 11 '13 at 17:27
  • Well, suit yourself, as long as you don't work with me on the same project... – Nic Wortel Dec 11 '13 at 17:35
  • Have you every worked with WordPress @nic, because that is how most of their code is ie `the_title();` or you can attach it to a string. It is the a time savor if you are using several instances of the function through out your script with the same outputs needed. – Denver William Dec 11 '13 at 17:40
  • WordPress isn't really a good coding example (understatement). Which is why I don't use it. – Nic Wortel Dec 11 '13 at 17:57
  • @nic well give a chance to save a hundred characters in thousands of lines of code, I would choose to save some characters. So I guess I will never be working for you! – Denver William Dec 11 '13 at 17:59
  • I understand that, and I would have thought the same way a couple of years ago. However, once I started using concepts like Object-oriented Programming, and the Model-View-Controller (MVC) pattern, I stopped using `echo` within functions (and methods) wherever I could. Believe me, even though I had to type some more characters, it made life a lot easier. In the end, good standards save you more time than trying to keep your code as short as possible. – Nic Wortel Dec 11 '13 at 18:06
  • typing `$get_info->data();` is still way less code than typing `echo $get_info->data();` Maybe one day I will change, but not this day. But I am thinking about getting into more object oriented programming. – Denver William Dec 11 '13 at 18:08

1 Answers1

2

Owing to PHP's rules for boolean comparisons, the non-numeric string "load" compares truthfully to the integer 0. So your first condition will match any string that can't be converted directly to a non-zero integer. Instead of loosely comparing via ==, use a strict comparison via ===.

function get_info($key, $output = 0)
{
    $key = "_" . strtoupper($key);

    // Strict comparisons should be used to avoid strings matching int 0
    if ($output === "echo" || $output === 0) {
        echo "echo " . constant($key); 
    } elseif ($output === "load" || $output === 1) {
        return "load " . constant($key);
    }
}

An aside, not relevant to your question, but the alternate flow control style in PHP (if/elseif/endif and its loop cousins) are mainly intended for templating inside HTML. It's sort of unconventional to use them in straight PHP, where using the standard curly brace syntax is preferred. (if {}). If you're going to integrate with others' code, consider using the standard syntax instead.

Nic Wortel
  • 11,155
  • 6
  • 60
  • 79
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390