Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kordix's avatar

!= or <> does not work in @if

When I use

@if($variable <> 0)   text @endif

or

@if($variable != 0)   text @endif

In blade file it does not work , other comparisions work perfectly. You know anything about that?

0 likes
13 replies
jlrdw's avatar

Should work is the variable set?

shez1983's avatar

dump out variable just before this line..

Snapey's avatar

Only the second form is valid

or

@if(!$variable == 0)   text @endif
jlrdw's avatar

Yes it would help to know what the variable is.

If still in a string format

@if($variable <> '0')   text @endif 

Should definitely work. If the variable is set that is.

But I am referring to PHP I have no idea about blade.

Snapey's avatar

Blade statements are php, D'oh !

I never use <> in a php context but it is valid

$a != $b Not equal TRUE if $a is not equal to $b after type juggling.

$a <> $b Not equal TRUE if $a is not equal to $b after type juggling.

... after type juggling. Quoting 0 makes no difference

jlrdw's avatar

I just did this:

    public function testVar()
    {
        $variable = 1;
        if ($variable <> 0){
            echo 'I ain\'t zero';
        }
    }

When ran I got

I ain't zero

So that's telling me something is going on in blade that isn't working right.

OP, try stright php in the view just to see if it works, we do not know where that variable came from.

I will try it in blade, I don't use blade so will take a few minutes.

jlrdw's avatar

@kordix to inform you

@if($variable <> 0)   text @endif

does work in blade, I just:

    public function testVar()
    {
        $variable = 1;
        if ($variable <> 0){
            echo 'I ain\'t zero';
        }
        return view('dog.test')->with('variable', $variable);
    }

And in view:

<html>
    <head>
        <title></title>
    </head>
    <body>


        <div class="container">
            @if($variable <> 0)
              not zero
            @endif
        </div>
    </body>
</html>


And I got:

not zero      

As expected.

I had to dig into docs to do that, I don't use blade, except for custom pagination templates.

Snapey's avatar

@jlrdw

What are you on buddy?

Why don't you just wait for the OP to reply rather than keep reinforcing that you don't know what blade actually does.

This

        <?php $variable=1 ?>
        @if($variable != 0)   text @endif

Compiles in the view to

        <?php $variable=1 ?>
        <?php if($variable != 0): ?>   text <?php endif; ?>

You see.... PHP

Now, perhaps if the OP is actually putting this code in an area of the page that is not actually displayed.....

jlrdw's avatar

@snapey because you said earlier

Only the second form is valid

So I was testing I did not think they had taken the first form out of PHP.

The point being

<>

Is good. And asking what I'm on, well it's a two way street, you were the one who said the quote above. So that made me curious. Maybe both of us should wait.

kordix's avatar

I swear on God

dd($drzwi->wizjer) 

shows "S"

@if($drzwi->wizjer <> 0) Wizjer @endif

does not show anything. This works:

@if($drzwi->kontaktron == 1)Kontaktron @endif 

just I swear on God next to it in the same place same with !=

EDIT holu shit the quotes ("0") worked

jlrdw's avatar

@kordix read @cronix answers above, it makes sense that it's a pdo driver, well maybe.

@kordix so are things working now, or are you still having problems.

Please or to participate in this conversation.