9

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';

AlxVallejo
  • 3,066
  • 6
  • 50
  • 74

8 Answers8

22

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.

usumoio
  • 3,500
  • 6
  • 31
  • 57
  • for laravelers: there is a default function under \PHPUnit\Framework\Constraint namespace : **isTrue($a_value)** – CodeToLife Aug 09 '21 at 13:05
11

Testing $var == true is the same than just testing $var.

You can read this SO question on comparison operator. You can also read PHP manual on this topic.

Note: a variable does not return true. It is true, or it evaluates to true. However, a function returns true.

Community
  • 1
  • 1
Guillaume Poussel
  • 9,572
  • 2
  • 33
  • 42
9

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!";
Jezen Thomas
  • 13,619
  • 6
  • 53
  • 91
2
if($add_visits === TRUE)

should do the trick.

Dilvish5
  • 310
  • 4
  • 12
2

The most secure way is using php validation. In case of a ajax post to php:

$isTrue=filter_var($_POST['isTrue'], FILTER_VALIDATE_BOOLEAN);
Flemming
  • 694
  • 7
  • 22
1

Yeah, that would work fine.

//true
if($variable) echo "success";
if($variable == true) echo "success";


//false
if(!$variable) echo "failure";
if($variable == false) echo "failure";
Ben
  • 5,627
  • 9
  • 35
  • 49
0
if (isset($add_visits) && $add_visits === TRUE){
     echo 'success';
}

Might seem redundant, but PHP will throw a Notice if $add_visits isn't set. This would be the safest way to test if a variable is true.

cdmo
  • 1,239
  • 2
  • 14
  • 31
0

Noncompliant Code Example

if ($booleanVariable == true) { /* ... */ }
if ($booleanVariable != true) { /* ... */ }
if ($booleanVariable || false) { /* ... */ }

doSomething(!false);

Compliant Solution
if ($booleanVariable) { /* ... */ }
if (!$booleanVariable) { /* ... */ }
if ($booleanVariable) { /* ... */ }

doSomething(true);

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 18 '22 at 07:37