PHP grabbing arrays to create lines of code

前端 未结 4 631
别跟我提以往
别跟我提以往 2021-01-17 07:21

I would like to create something simple. Since I\'m new to PHP, I\'m struggling to see what I\'m doing wrong,. I have two PHP files content in code snippets below, as well a

相关标签:
4条回答
  • 2021-01-17 07:39

    Mixing string and PHP code is confusing, especially when you have double quotes, it's easier to put HTML strings (which usually contain double quotes) in single quotes and PHP expressions outside of these strings:

    foreach ($toppingItems as $item) {
        echo '<label class="hover topping" for="c'.$item['number'].
            '"><input class="items" onclick="findTotal()" type="checkbox" '.
            'name="topping" value="1.00" id="c'.$item[number].'">'.
            $item[title].'</label>';
    }
    
    0 讨论(0)
  • 2021-01-17 07:39

    your code

    foreach ($toppingItems as $item) {
        echo "<label class="hover topping" for="c$item[number]"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c$item[number]">$item[title]</label>";
    }
    

    contains an error:

    You cannot use unescaped double quotes within a double quoted PHP string.

    Try:

    <?php foreach ($toppingItems as $item) { ?>
        <label class="hover topping" for="c<?= $item[number] ?>">
            <input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c<?= $item[number]?>"><?= $item[title] ?>                 
        </label>
    <?php } ?>
    

    As you can see I've used to set my control structure, yet I've place my HTML outside of the php open and closing tag. Also I've used

    <?= $item[number] => 
    

    as short for

    <?php echo $item[number] ?>
    

    This is a matter of preference, I find it makes the code more readable.

    0 讨论(0)
  • 2021-01-17 07:41

    you're using double quotes " inside double quotes "

    echo "<label class="hover topping" for="c$item[number]"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c$item[number]">$item[title]</label>";
    

    try wrapping your HTML attributes with single quotes '

    echo "<label class='hover topping' for='c$item[number]'><input class='items' onclick='findTotal()' type='checkbox' name='topping' value='1.00' id='c$item[number]'>$item[title]</label>";
    

    thank Fred for pointing out that if there's any possibility your values, such as $item, could contain a single quote ' themselves, then you'll have to escape the inner double quotes with a backslash \"

    echo "<label class=\"hover topping\" for=\"c$item[number]\"><input class=\"items\" onclick=\"findTotal()\" type=\"checkbox\" name=\"topping\" value=\"1.00\" id=\"c$item[number]\">$item[title]</label>";
    

    and just to take it one step further, if there's any chance that $item could contain a mix of both single ' and double " quotes, then you'll have to take the split approach in @Kevinvhengst's answer and then wrap your variables with htmlentities

    echo '<label class="hover topping" for="c'.htmlentities($item['number']).'"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c'.htmlentities($item['number']).'">'.htmlentities($item['title']).'</label>';
    
    0 讨论(0)
  • 2021-01-17 08:01

    Echo the following:

    echo '<label class="hover topping" for="c'.$item['number'].'"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c'.$item['number'].'">'.$item['title'].'</label>';
    
    0 讨论(0)
提交回复
热议问题