Q:

Foreach in php not working

I am getting an error

undefined variable label

I don't know why. I am using a foreach($labels as $label), but its not sending the data into label. I used var_dump($labels) and I got a whole array, so the problem doesn't seem to be with $labels, why doesn't the foreach to $label work?
Here is my code:
foreach($labels as $label) { ?>
  <tr>
    <td>img src=<?= $label["image"] ?>  </td>
    <td>50 cents</td>
    <td>$<?= $label["quantity"] ?> </td>
    <td>$<?= $label['quantity']*.5 ?></td> 
  </tr>
<? }  

Here is the var_dump:
array(11) { [0]=> array(2) { ["image"]=> string(14) "circles/L3.jpg"    ["quantity"]=> string(2) "72" } [1]=> array(2) { ["image"]=> string(14) "circles/L2.jpg" ["quantity"]=> string(2) "24" } [2]=> array(2) { ["image"]=> string(11) "TAGS/T4.jpg" ["quantity"]=> string(2) "72" } [3]=> array(2) { ["image"]=> string(14) "circles/L3.jpg" ["quantity"]=> string(2) "60" } [4]=> array(2) { ["image"]=> string(15) "circles/L11.jpg" ["quantity"]=> string(2) "72" } [5]=> array(2) { ["image"]=> string(12) "TAGS/T12.jpg" ["quantity"]=> string(2) "72" } [6]=> array(2) { ["image"]=> string(14) "circles/L3.jpg" ["quantity"]=> string(2) "36" } [7]=> array(2) { ["image"]=> string(14) "circles/L3.jpg" ["quantity"]=> string(2) "60" } [8]=> array(2) { ["image"]=> string(14) "circles/L3.jpg" ["quantity"]=> string(2) "36" } [9]=> array(2) { ["image"]=> string(14) "circles/L3.jpg" ["quantity"]=> string(2) "36" } [10]=> array(2) { ["image"]=> string(11) "TAGS/T3.jpg" ["quantity"]=> string(2) "60" } }  

here is the rest of the code:
  $id="";
  if(!empty($_GET['id'])){
 $id=$_GET['id'];
 }

$cs = "mysql:host=localhost;dbname=purimlabels";
$user = "seforim";
$password = '1234';

try {
    $options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
    $db = new PDO($cs, $user, $password, $options);
    $query='SELECT  image,quantity  FROM labels WHERE  CustomerID=? AND    Submitted="NO" ';
    $statement = $db->prepare($query);
    $statement->bindvalue(1,$id);
    $statement->execute();
    $labels = $statement->fetchAll(PDO::FETCH_ASSOC);
    $statement->closeCursor(); 
    }catch(PDOException $e) {
    die("Something went wrong " . $e->getMessage());
   }

A:

May be you cannot use short tag <? on your local environment. Check short_open_tag = On value in your local php.ini file.
You may use below code with php tag, generally short open tags are disabled because they can cause issues reading xml, so i would personally opt for the second option
<table>
<?php foreach($labels as $label): ?>
  <tr>
    <td><img src="<?php echo $label['image']; ?>" /></td>
    <td>50 cents</td>
    <td>$<?php echo $label['quantity']; ?> </td>
    <td>$<?php echo $label['quantity'].'*.5'; ?></td> 
  </tr>
<?php endforeach; ?>
</table>

