-10

Is it same in PHP:

if($x!=5)
{
 //code
}

VS

$x=5;
if(!$x)
{
 //code
}

What about if($x)? Expression in IF statement evaluates to either TRUE or FALSE unlike C where it is either 0 or anything other than 0 (say 1 or more). We can test the expression by using var_dump(!$x) in PHP. So,what about if($x)?

Parveez Ahmed
  • 1,325
  • 4
  • 17
  • 28

2 Answers2

0

No,it is not same in PHP:

Logical Operator.

! $x Not TRUE if $x is not TRUE.

Comparison Operators

$x!=5  Not equal  TRUE if $x is not equal to 5 

Source: PHP Documentation.

Sobin Augustine
  • 3,639
  • 2
  • 25
  • 43
0

They are not the same.

The first block of code tests whether or not the variable x does not equal 5.

The 2nd block of code tests whether x is not true. Since you declared a value for $x, the statement will be evaluated as false and the content inside the brackets will not execute.

Philip7899
  • 4,599
  • 4
  • 55
  • 114