Do these two php functions produce the same result?

2443 views php
4

I am currently working my way through a PHP course and am curious as to if these two functions produce the same result, is one more efficient then the other? If so please explain, thank you.

public function is_logged_in(){
        if ($_SESSION['user_id'] == true){
            return true;
        }else{
            return false;
        }
    }


      public function is_logged_in(){
        if (isset($_SESSION['user_id'])){
            return true;
        }else{
            return false;
        }
    }

answered question

why is $_SESSION['user_id'] a boolean?

2 Answers

5

Assuming $_SESSION['user_id'] is a typical numeric ID and is not equal to 0, then yes. The only scenario that this is not the same is when $_SESSION['user_id'] is literally false or is 0.

posted this
11

No they are not the same, you can test this

<?php
$falseVar = false;
var_dump(isset($falseVar));

this will return true becuase a variable contains false is set.

so if your $_SESSION['user_id'] tested with isset, it will return true whither $_SESSION['user_id'] is true or false

posted this

Have an answer?

JD

Please login first before posting an answer.