Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this if require a default?

This is the if:

Session::has('tk') ? Session::get('tk') : 1;

I would like to skip the : 1 but then it fails for some reason while tk is allways set in another if?

like image 629
Graham Avatar asked Sep 17 '26 14:09

Graham


2 Answers

if you already know that tk has a value, then you don't need to use has() function to check it.

Use just:

Session::get('tk');
like image 193
cyborg86pl Avatar answered Sep 19 '26 02:09

cyborg86pl


In a short if, you need to define both:

<?php
    $var = ($someThing) ? true: false;

If you don't want to do that, take a regular if-statement

<?php
    if ($someThing) {
        $var = true;
    }
like image 20
toesslab Avatar answered Sep 19 '26 04:09

toesslab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!