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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:17:52+00:00 2026-06-17T09:17:52+00:00

my code: if (isset($dayMarks[$res[‘resId’]][$dDate])) { $var=$dayMarks[$res[‘resId’]][$dDate]; echo $var; } note that the isset condition

  • 0

my code:

if (isset($dayMarks[$res['resId']][$dDate])) {
     $var=$dayMarks[$res['resId']][$dDate];
     echo $var;
}

note that the isset condition is identical to the value assigned to $var, which creates a pretty ugly code.

How is it possible to assign the condition to $var without repeating it?

(in javascript, I’d write if (var=$dayMarks[$re...) )

  • 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-17T09:17:53+00:00Added an answer on June 17, 2026 at 9:17 am

    This is a common problem in PHP where including files can create uncertainty about variables.

    There are a two approaches that work well for me.

    Default Assignment

    With default assignment the $var variable will be given a default value when the key doesn’t exist.

    $var = isset($dayMarks[$res[‘resId’]][$dDate]) ? $dayMarks[$res[‘resId’]][$dDate] : false;

    After this code can assume that $var will always contain a valid value.

    Default Merger

    My preferred method is to always declare a default array that contains all the required values, and their defaults. Using the False value to mark any keys that might be missing a value (assuming that key holds another value type besides boolean).

    $default = array(
        'date'=>false,
        'name'=>'John Doe'
    );
    
    $dayMarks[$res['resId']] = array_merge($default, $dayMarks[$res['resId']]);
    

    This will ensure that the required keys for that variable exist, and hold at least a default value.

    You can now test if the date exists.

    if($dayMarks[$res['resId']]['date'] !== false)
    {
         // has a date value
    }
    

    While this might not work exactly for your array. Since it looks like it’s a table structure. There is a benefit to switching to named key/value pairs. As this allows you to easily assign default values to that array.

    EDIT:

    The actual question was if it was possible to reproduce the JavaScript code.

    if (var=$dayMarks[$re…)

    Yes, this can be done by using a helper function.

    NOTE: This trick should only be used on non-boolean types.

     function _isset($arr,$key)
     {
        return isset($arr[$key]) ? $arr[$key] : false;
     }
    
     $a = array('zzzz'=>'hello');
     if(($b = _isset($a,'test')) !== false)
     {
        echo $b;
     }
     if(($c = _isset($a,'zzzz')) !== false)
     {
        echo $c;
     }
    

    See above code here

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

Sidebar

Related Questions

I have some code that checks for a value of say, var in the
I am trying to get the following code to work: if (isset($_POST['file'])){ if(file_exists($_POST['file'])){ echo
I have code like this: <?php if(isset($global_info_results)): ?> <?php echo count($global_info_results) ?> <span>Mali Oglasi:
I have come across scripts that use: isset($_POST['submit']) as well as code that uses:
Here is the code: if (isset($_POST['state']) && count($err) > 0) { echo <p>; array_walk($err,
Using the following code: if (isset($_POST['delete']) && isset($_POST['id'])) { $first = get_post('first'); $query =
I have the following code: if ( (isset($_GET['slAction'])) && ($_GET['slAction'] == manage_label) ) {
Okay so i have this code: if (isset($_GET['book'])) { $query= SELECT book_id, title, authors.`author`
I am using this PHP code: if (isset($_GET['c'])) { $pages = array(home, upload, signup);
I am getting this error here is my code if(isset($_POST['submit'])) { $projTit=mysql_escape_string($_POST['projecttitle']); $projCat=mysql_escape_string($_POST['projectcategory']); $budget=intval(mysql_escape_string($_POST['budget']));

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.