How to include php code within DELIMITER?

Asked

Viewed 32 times

0

I need some help, I would like to include a code within DELIMETER, but I’m not succeeding, someone can help me or propose another alternative. The function below shows all the quantity I have in stock, a customer’s way to choose the quantity at checkout!

Follow my code below:

$product = <<<DELIMETER
 <a class="btn btn-default">
 <select id ="quantidade" name="quantidade"> <?PHP for ($i = 0; $i <=$row['produto_quantidade']; $i++) echo "<option value=".$i.">".$i."</option>";
 ?>  </select> </a>
DELIMETER;
  • What are you trying to do? Your "delimiter" is a string heredoc. Do you want to put a select with all the options inside it? That’s it?

  • Heredoc behaves like any string, there is no way to execute a PHP code inside it. You need to work with string concatenation.

1 answer

2

heredoc is a multiline string, you must concatenate the results and add them as follows:

$options = "";

for ($i = 0; $i <=10; $i++) {
    $options .= "<option value='{$i}'>{$i}</option>";
}

$product = <<<DELIMETER
 <a class="btn btn-default">
 <select id ="quantidade" name="quantidade"> {$options}  </select> </a>
DELIMETER;

var_dump($product);

Exit:

string(400) " <a class="btn btn-default">
 <select id ="quantidade" name="quantidade"> <option value='0'>0</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option>  </select> </a>"

See on ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.