Alex
Alex

Reputation: 9265

Ternary operator to evaluate to true if string or false if bool

function file_get_contents_new($url, $wait = 3) {
    if (http_response($url, '200', $wait)) {
        return file_get_contents($url);
    } else {
        return FALSE;
    }
}
function OO_chart($query_stub, $length)
    {
        $key = $this->OO_charts_key;
        $url_api = "https://api.oocharts.com/v1/query.jsonp?query={$query_stub}&key={$key}&start={$length}";
        $json = file_get_contents_new($url_api);
        if (file_get_contents_new($url_api) ? (string) true : (bool) false && (bool) $this->OO_active) {
            return json_decode($json, TRUE);
        } else {
            $msg = new Messages();
            $msg->add('e', 'JSON error. Check OOcharts api.');
            redirect('index.php');
        }
    }

Will file_get_contents_new($url_api) ? (string) true : (bool) false work in this way? As in, will it evaluate to true if the function outputs a string, and will it evaluate to false if the function is bool?

Upvotes: 0

Views: 1417

Answers (3)

Aris
Aris

Reputation: 5055

file_get_contents_new() returns the read data or FALSE on failure.

http://php.net/manual/en/function.file-get-contents.php

So why complicate things? This should work. But there is only one way to find out...

    $contents = file_get_contents_new($url_api);

    if (($contents!==false) && (bool) $this->OO_active) {
         return json_decode($json, TRUE);
    } 

Also I don't like the (bool) assignment. Isn't that parameter supposed to be boolean already?

And to answer your question - YES a ternary operator inside an if statement should work. But it's just hard to test, debug, maintain, and makes your code less readable. I don't like to use it like this.

Upvotes: 0

Martijn
Martijn

Reputation: 16113

No, that wont work. Translated back to a normal if/else it's easier to explain why this wont work:

if( !file_get_contents($file) ){
    // the file_get_contents function returned false, so something went wrong
}
else{
    // the if-condition was not met, so the else will do its job
    // The problem is that we got the content in the if-condition, and not in a variable
    // therefor we can not do anything with its contents this way
    echo "It did work, but I have no way of knowing the contents";
}

A solution could be like this:

$content = file_get_contents($file);
$content = $content===false ? 'No content' : $content; // rewrite if file_get_contents returns false

Be a little carefull with the ternary check, use triple equal signs. In some bizar situation the content of the file could be 'false'. Checking if $content==false would return true because it has the same value (but not the same type (string/boolean)

Upvotes: 1

1234567
1234567

Reputation: 956

No. Your trying to type-juggle (switching the data type of a variable) in the if(){}else{} statement.

The correct way to do this is to change your if statement to the following:

if (is_string(file_get_contents_new($url_api)) && is_bool($this->OO_active)) {
   return json_decode($json, TRUE);
} else {
   $msg = new Messages();
   $msg->add('e', 'JSON error. Check OOcharts api.');
   redirect('index.php');
}

Now, as you saw, I made use of the is_bool() and is_string() functions in PHP. If your function file_get_contents_new returns a string, it will evaluate to true, and check if $this->OO_active is a boolean or not. If your file_get_contents_new function returns a boolean (meaning it is NOT a string), it will immediately perform your else{} statement, because both of your if conditions must be true (because of the &&/and operators), and if one of those conditions returns false, or breaks the chain, it will move to the else statement.

Upvotes: 2

Related Questions