1

I've been struggling with this for at least an hour to no avail. I'm sure the answer is simple, but I can't figure it out. The foreach loop is what I'm trying to accomplish, but it doesn't work. The for loop works and renders fine. Please help! - Jon

<div class="td"><select name="vehYear">
    <?php
    foreach ($veh_year_array as $item)
    {?>
        <option value="<?php echo $item;?>"><?php echo $item;?></option>
    <?php}
    ?>

    <?php
    for ($i = date('Y'); $i > 1950; $i--) {
        echo "<option>$i</option>";
    }
    ?>
</select></div>
3
  • 4
    are you sure the $veh_year_array is populated? maybe try pasting the output of var_dump($veh_year_array)? Commented Sep 18, 2017 at 23:29
  • @starnzd - If you're doing an edit, it's OK to reformat the OP's code (add/remove line feeds and indentions to make it more readable), but it's not OK to change the original code (changing the actual syntax). Commented Sep 18, 2017 at 23:37
  • @MagnusEriksson cheers, good to know Commented Sep 19, 2017 at 0:00

1 Answer 1

1

I tested your code and it seems your error is the curly bracket }. In my environment I received an

invalid argument error

when pasting your original code. You want to be very careful in PHP since it is a sensitive language.

So exchange this

<?php}?>

to this

<?php }?>

Your code should then execute

<?php
$veh_year_array =array("Tomato", "Apple", "Orange");

  foreach ($veh_year_array as $item)
    {?>
        <option value="<?php echo $item;?>"><?php echo $item;?></option>
    <?php }?>

    <?php
    for ($i = date('Y'); $i > 1950; $i--) {
        echo "<option>$i</option>";
    }
    ?>
Sign up to request clarification or add additional context in comments.

4 Comments

@Baloon Fight. You're exactly right. I wouldn't have imagined such a small thing would make a huge difference. :-/ Cheers. Jonathan
Also, what environment are you using? I'm using intellij and there were no "invalid argument" errors thrown.
@StackBoy : I'm using Netbeans latest version. Make sure you test with ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); this will catch every PHP errors. Also if you use Mysqli use this mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); Place those pieces of code first thing on every PHP page. Cheers
Okay I'll try to incorporate the functions you included to catch PHP errors.

Your Answer

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