Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7907179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:18:35+00:00 2026-06-03T11:18:35+00:00

I am having problem setting a variable in PHP for multiple use into if

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:18:37+00:00Added an answer on June 3, 2026 at 11:18 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having problem setting up tomcat context variable. I have tried: in web.xml
I am having problems setting the educationflag variable to 1. The problem I am
I am having a problem setting the Authorize attribute Role value from a variable.
I'm having a problem setting a cookie and doing a 302 redirect In chrome
I am having a problem setting up the following in Visual Studio 2008: a
I am having a problem setting the background of UITableView to a UIImageView (see
hi forum member I am having one problem with setting the radiofield in the
I am having a problem where a cookie I am setting is being lost
Having a bit of a problem. Trying to assign a template variable within a
I am having a problem with setting an ImageView image to an image which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.