Home > Software engineering >  Notice: Undefined offset: 0 in and Undefined offset: 1 in php
Notice: Undefined offset: 0 in and Undefined offset: 1 in php

Time:01-29

I have a php code as shown below in which I am getting error Undefined offset: 0 at Line A and Undefined offset: 1 at Line B

<?php 
    $area = &$_SESSION['diagram']->areas[$index];

    // If a new one, create it and link in the session
    if (!is_object($area)) {
        $area = new DiagramArea();
        $_SESSION['diagram']->areas[$index] = &$area;
    }
    for ($i = 0; 5 > $i;   $i) {
    var_dump($area->files); // Line M                  
    ?>    
    <fieldset>
        <legend>Name / Pattern</legend>
        <input type="text" name="label<?php echo $i; ?>"
            value="<?php echo is_object($area->files[$i]) ? $area->files[$i]->label : ''; ?>" />    // Line A
        <input type="text" name="pattern<?php echo $i; ?>"  
            value="<?php echo is_object($area->files[$i]) ? $area->files[$i]->pattern : ''; ?>" />  // Line B
    </fieldset>
<?php } ?>

For debugging purpose, I have added Line M in the code above. At Line M, I am getting the following o/p:

./abc.php:99:
array (size=0)
  empty
  

Problem Statement:

I am wondering what changes I need to make at Line A and Line B so that I can avoid the warning in both of those lines.

CodePudding user response:

Because you are performing an is_object test, it might be worth it to make sure your object matches what you expect by performing a property_exists test, too. However, it might be overkill for this, too (although it doesn't hurt).

This version breaks things on to multiple lines which I personally believe makes it more readable. It also uses the alternative control structure for PHP which doesn't use curl braces. This is optional, but, once I again, I personally find it easier to read when switching between HTML and PHP so that I don't need to chase curly braces around. Lastly, I try very hard to not perform a lot of logic within an HTML attribute, and instead just echo. The reason for this is that when debugging, the HTML parsing algorithm might hide PHP errors in the browser.

<?php for ($i = 0; 5 > $i;   $i) : ?>
    <fieldset>
        <legend>Name / Pattern</legend>
        <input
                type="text"
                name="label<?php echo $i; ?>"
            <?php if (isset($area->files[$i]) && is_object($area->files[$i]) && property_exists($area->files[$i], 'label')): ?>
                value="<?php echo $area->files[$i]->label; ?>"
            <?php endif; ?>
        />
        <input
                type="text"
                name="pattern<?php echo $i; ?>"
            <?php if (isset($area->files[$i]) && is_object($area->files[$i]) && property_exists($area->files[$i], 'pattern')): ?>
                value="<?php echo $area->files[$i]->pattern; ?>"
            <?php endif; ?>
        />
    </fieldset>
<?php endfor; ?>

Make sure that if you use this that you don't accidentally miss the closing /> on the input elements.

  •  Tags:  
  • php
  • Related