Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if variable returns true

Tags:

php

I want to echo 'success' if the variable is true. (I originally wrote "returns true" which only applies to functions.

$add_visits = add_post_meta($id, 'piwik_visits', $nb_visits, true);
if($add_visits == true){
         echo 'success';
}

Is this the equivalent of

$add_visits = add_post_meta($id, 'piwik_visits', $nb_visits, true);
if($add_visits){
         echo 'success';
}

Or does $add_visits exist whether it is 'true' or 'false';

like image 712
AlxVallejo Avatar asked Sep 20 '12 18:09

AlxVallejo


3 Answers

You might want to consider:

if($add_visits === TRUE){
     echo 'success';
}

This will check that your value is TRUE and of type boolean, this is more secure. As is, your code will echo success in the event that $add_visits were to come back as the string "fail" which could easily result from your DB failing out after the request is sent.

like image 165
usumoio Avatar answered Oct 04 '22 18:10

usumoio


They're the same.

This...

if ($add_visits == true)
    echo 'success';

...Is the same as:

if ($add_visits)
    echo 'success';

In the same fashion, you can also test if the condition is false like this:

if (!$add_visits)
    echo "it's false!";
like image 22
Jezen Thomas Avatar answered Oct 04 '22 20:10

Jezen Thomas


if($add_visits === TRUE)

should do the trick.

like image 26
Dilvish5 Avatar answered Oct 04 '22 18:10

Dilvish5