-1

I'm developing a PHP script for my online store and I'm getting this error when I try to use Heredocs. When I delete the $prodotti variable, everything works fine.

<?php
function show_products(){
    global $connection;

    $query = "SELECT * FROM prodotto";
    $result = mysqli_query($connection, $query) or die(mysqli_error($connection));

    if($num_rows = mysqli_num_rows($result)){
        while($row = mysqli_fetch_assoc($result)){
          $prodotti = <<<STRINGA_PDT

          <div class="col-lg-4 col-md-6 mb-4">
          <div class="card h-100">
          <img class="card-img-top" src="../risorse/IMAGES/{$row['immagine']}" alt="">
            <div class="card-body">
              <h4 class="card-title">
                <a href="prodotto.php?id={$row['id_prodotto']}">{$row['nome_prodotto']}</a>
              </h4>
              <h5>€{$row['prezzo']}</h5>
              <p class="card-text">{$row['descr_prodotto']}</p>
            </div>
            <div class="card-footer text-center">
            <a href="carrello.php?add={$row['id_prodotto']}"><button type="button" class="btn btn-primary btn-small">Acquista</button></a>
            <a href="prodotto.php?id={$row['id_prodotto']}" class="btn btn-success btn-small">Dettagli</a>
            </div>
          </div>
        </div>

          STRINGA_PDT;
        }
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Feb 27 '20 at 22:34

1 Answers1

1

I think it's very simply a case of making sure the marker for the end of the HEREDOC is at the start of the line

    <?php
    function show_products(){
        global $connection;

        $query = "SELECT * FROM prodotto";
        $result = mysqli_query($connection, $query) or die(mysqli_error($connection));

        if($num_rows = mysqli_num_rows($result)){
            while($row = mysqli_fetch_assoc($result)){
              $prodotti = <<<STRINGA_PDT

              <div class="col-lg-4 col-md-6 mb-4">
              <div class="card h-100">
              <img class="card-img-top" src="../risorse/IMAGES/{$row['immagine']}" alt="">
                <div class="card-body">
                  <h4 class="card-title">
                    <a href="prodotto.php?id={$row['id_prodotto']}">{$row['nome_prodotto']}</a>
                  </h4>
                  <h5>€{$row['prezzo']}</h5>
                  <p class="card-text">{$row['descr_prodotto']}</p>
                </div>
                <div class="card-footer text-center">
                <a href="carrello.php?add={$row['id_prodotto']}"><button type="button" class="btn btn-primary btn-small">Acquista</button></a>
                <a href="prodotto.php?id={$row['id_prodotto']}" class="btn btn-success btn-small">Dettagli</a>
                </div>
              </div>
            </div>

STRINGA_PDT;
            }
        }
    }

Anything else will cause PHP to fail to recognise it as a Heredoc

Ben Hillier
  • 2,126
  • 1
  • 10
  • 15
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Feb 27 '20 at 22:35
  • 1
    *"The closing marker for doc strings is no longer required to be followed by a semicolon or newline. Additionally the closing marker may be indented, in which case the indentation will be stripped from all lines in the doc string."* https://www.php.net/manual/en/migration73.new-features.php – Dharman Feb 27 '20 at 22:39
  • @Dharman I never knew that! Thank you. I assume the OP wasn't using PHP 7.3 then :-) – Ben Hillier Feb 28 '20 at 07:45