0

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 enter image description here

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.

1 Answer 1

4
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.