I am having problem setting a variable in PHP for multiple use into if statements
.
Although there are many files in my layout but these are the 3 files relevant to this issue:
session.php (it is actually a JavaScript function which determines the browser-width of the visitor which is included in my javascript file and I call this via an ajax call. However since that is irrelevant to this issue hence to avoid confusion and too much code on this page I am avoiding that file.)
<?php
session_start();
$_SESSION['layoutType'] = $_REQUEST['layoutType'];
?>
core.php
<?php
session_start();
if (empty($_SESSION['layoutType'])) {
echo '<script type="text/javascript"> var session=false; var layoutType;</script>';
} else {
echo '<script type="text/javascript"> var session=true; var layoutType=' . $_SESSION['layoutType'] . ';</script>';
}
$layout = 'normal';
if (!empty($_SESSION['layoutType'])) {
$layoutType = $_SESSION['layoutType'];
if ( $layoutType <= 219 ) {
$layout = 'minimal';
} else if ($layoutType >= 220 && $layoutType <= 1024 ) {
$layout = 'small';
} else {
$layout = 'normal';
}
$_SESSION['layout'] = $layout;
}
index.php
<?php
include ('core/core.php');
// Function to load all the JavaScript files
getJS();
echo '<div>Your browser width is: ' . $_SESSION['layoutType'] . ' and Value of $layout variable currently is <strong>'.$layout.'</strong></div>';
if ($layout = 'normal') { ?>
<?php echo '<strong>' . $layout . '</strong> is the value of $layout in session'; ?>
<div id="sidebar">
<p>Widgets go here</p>
</div>
<?php } ?>
The output
Even though, The layout-width falls in the small category (Your browser width is: 872 and Value of $layout variable currently is small) , it is still displaying the bit which it should display only is the layout-widthe category is normal (normal is the value of $layout in session) .
Typically what is hapenning here is the value of $layout
is being overwritten by the if statement
in the index.php
file.
What I am looking for?
Along with layoutType
I also want the value of $layout
to be set in the session based on the logic in core.php
. So that I can use it multiple times across the website in various if statements
.
This is very important because these are varioys widgets / content in my layout which I want to load / not-load depending on the device of my visitor. Hence almost all the widhets wil be wrapped into an if statement
.
Kindly help.
if ($layout = 'normal') { ?>
Evaluates to true because it's an assignment (use ==
or ===
instead). You can find more information about the Comparison operators in the PHP manual .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.