Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kgp43's avatar
Level 2

If statement within form input value

Hi,

How should I hand this? (see code below). No value is being displayed, i guess because the if statement is within the " ".

<input type="text" class="form-control" value="
@if($product->salesprice > 0)
    {{ round((($product->salesprice - (($price->amount*7.5)+($price->shipment_cost/$price->purchase_num)))/$product->salesprice)*100) }}
@endif
" disabled>
0 likes
5 replies
bobbybouwmann's avatar

Instead of the if you should use a ternary operator.

<input type="text" class="form-control" value="{{ $product->salesprice > 0 ? round((($product->salesprice - (($price->amount*7.5)+($price->shipment_cost/$price->purchase_num)))/$product->salesprice)*100) : '' }}" disabled>
1 like
bobbybouwmann's avatar
Level 88

It might even be better to pull this calculation out of the input and put it above that, in a controller or somewhere else

<?php $price = $product->salesprice > 0 
    ? round((($product->salesprice - (($price->amount*7.5)+($price->shipment_cost/$price->purchase_num)))/$product->salesprice)*100)
    : null; ?>
<input type="text" class="form-control" value="{{ $price }}" disabled>
kgp43's avatar
Level 2

The PHP solution will fix it nicely I think :) Thanks for the help.

sanjivst's avatar

Thank You Ternary Operator solved my problem.

Please or to participate in this conversation.