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 8397799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:52:58+00:00 2026-06-09T20:52:58+00:00

How can I set one variable to true based on other conditions. Here, instead

  • 0

How can I set one variable to true based on other conditions. Here, instead of doing

if ($vara && $varb && $varc)

I’m something like below. Problem is, I’m just not getting something right. Can you please help me?

<?php

$onward = false;

$vara = 11;
$varb = 21;
$varc = 3;

if ($vara == 1)
{$onward = true;}else{$onward = false;}

if ($varb == 2)
{$onward = true;}else{$onward = false;}

if ($varc == 3)
{$onward = true;}else{$onward = false;}

if ($onward)
{
echo "Ok";
}else {echo "Not ok";}

?>
  • 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-09T20:52:59+00:00Added an answer on June 9, 2026 at 8:52 pm

    Each of your conditions ignores the result of the previous condition. You need to include the previous state of $onward in each subsequent test:

    if ($vara == 1)
    {$onward = true;}else{$onward = false;}
    
    if ($varb == 2 && $onward)
    {$onward = true;}else{$onward = false;}
    
    if ($varc == 3 && $onward)
    {$onward = true;}else{$onward = false;}
    

    This way, varb and varc are only tested if $onward is still true after the previous test.

    This is a particularly ugly way of writing code. If you have three large conditions and you don’t simply want to join them on one line as in your $vara && $varb && $varc, you should be writing it this way:

    $onward = ($vara == 1)
    $onward = $onward && ($varb == 2);
    $onward = $onward && ($varb == 3);    
    

    Any time you’re simply returning/setting something to true/false in the branches if your if statement, you should just be returning/setting the condition itself.

    That is, this:

    if (condition) {
      return true;
    } else {
      return false;
    }
    

    should always be written:

    return condition;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a Zend_Form with a Zend_Form_Element_Select on it, i can set ONE option selected
In Linux, can one set a custom icon for a folder/directory as it appears
When sending TCP packets over a high latency network, one can set the TCP
How does one build a function that can set an arbitrary value within a
How can I set all images in one line and display only the first
We can prove that set of all one argument functions cannot be countable using
Can any one please tell me how to set notifications for different values at
Any one can share the mainfest.xml. How to set it? then can remove the
there is one think, i can't understand anyway:((( when i try to set cookie(it
How can you have only one declaration of the database connection variable, $dbconn ?

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.