Lewes
Lewes

Reputation: 123

Checking a boolean after returning a boolean from a function

I am utterly confused: why won't my method of function work?

I have a slight problem, I am not sure where it is coming from though, but I'd like to check first if my IF statements are the cause, yet in java if(var){ //var is true } works, yet in PHP, it seems more complex than my understanding of booleans.

I am attempting to check if a boolean is true after getting return true; from a function I've used in check.

Some of my code is this.

IF: if($user->userValidMC($_POST['username']) === true FUNCTION:

    public function userValidMC($user){

        // grab data
        $minecraftOutput = file_get_contents('https://www.minecraft.net/haspaid.jsp?user=' . $user . '');

        return $minecraftOutput;

    }

Am I checking something wrong, or is my code completely wrong (the function)?

Upvotes: 1

Views: 114

Answers (6)

hek2mgl
hek2mgl

Reputation: 157947

The method will actually return a string. (have tested with this url) Change the if to:

if($user->userValidMC($_POST['username']) !== 'false' 

This is because if the user exists, the method will return the page content and not a boolean true. But you are testing for === true. But if the user doesn't exists the method will return false as file_get_contents() fails. So you should check for that.

Another way - which I would prefer - is to change the method to:

public function userValidMC($user){
    // grab data
    // the page will return the string 'false' if the user does not exist
    $minecraftOutput = file_get_contents('https://www.minecraft.net/haspaid.jsp?user=' . $user . '');
    return $minecraftOutput !== 'false';
}

... and the (original) if statement:

if($user->userValidMC($_POST['username']) === true 

Upvotes: 3

Dan
Dan

Reputation: 2302

The 'file_get_contents' returns a string on success and false on failure.

If you're simply testing that you get a valid response from the call then:

return $minecraftOutput !== false;

Will return a boolean value.

Or if you're checking for a specific value in the response then:

return $minecraftOutput === "expectedValue";

If you wanted to be less strict then:

if($user->userValidMC($_POST['username']) == true)

Will pass (most) strings with a length larger than 0, but returning a strict boolean value out of your function is safer.

See: PHP type comparisons

Upvotes: 0

cernunnos
cernunnos

Reputation: 2806

The if you posted checks wether the function "userValidMC" has returned true, note that you are using ===, so you are checking for the actual "true" boolean value, and not just wether the value evaluates to true (using ==).

if($user->userValidMC($_POST['username']) === true)

However, that function returns the result of file_get_contents (http://php.net/manual/en/function.file-get-contents.php)

This function returns the contents of the file or false on failure, so it should never return true.

You can check the result this way:

if($user->userValidMC($_POST['username']) !== false)

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173522

The remote service returns a string, either 'true' or 'false', so the return value should be:

return trim($minecraftOutput) === 'true';

Upvotes: 2

Ryan
Ryan

Reputation: 3582

file_get_contents will return either the contents of the file, or false.

Using triple = means compare value and type, so you are saying if that functions returns a boolean of TRUE, which it won't,

Upvotes: 0

Trenton Trama
Trenton Trama

Reputation: 4930

You're returning the results of file_get_contents.
That contains the contents of the file, not a boolean value.
I would suggest that you check to see if the file exists and then see if there is a length to the content.

Upvotes: 0

Related Questions