I want to include 2 files based on the following conditional statement
<?php
if (isset($_SESSION['name']) && $_SESSION['name'] == true) {
include 'file_a.php';
}else{
include 'file_b.php';
}
?>
Is this the correct way?
I want to include 2 files based on the following conditional statement
<?php
if (isset($_SESSION['name']) && $_SESSION['name'] == true) {
include 'file_a.php';
}else{
include 'file_b.php';
}
?>
Is this the correct way?
You can try next code:
if(session_id() == '') {
session_start();
include !empty($_SESSION['name']) ? 'file_a.php' : 'file_b.php';
}
It depends what you mean by 'correct':
If you mean 'is it valid' then yes, your code will work and there are no syntax errors.
If you mean 'can it be prettier', then perhaps it could, but it's personal preference whether you'd want to use ternary operators or not:
$toShow = isset($_SESSION['name']) && $_SESSION['name'] ? "file_a.php" : "file_b.php";
include $toShow;
Again, whether or not this is better or worse than your previous code, is down to your personal opinion.
TL;DR: Yes, your code is correct.