html - Fatal error: Can't use function return value in write context in views/stock/form.php on line 29

398

My code is working just fine in localhost , but when i upload it to the server, it show this error on spesific line. This is the error line of code.

<input type="text" required="required" name="i_name" class="form-control" placeholder="Masukkan nama item..." <?php if(!empty(trim($row->item_name))){ ?> value="<?= $row->item_name ?>"<?php } ?> />
411

Answer

Solution:

That's because you are usingempty() on thetrim() function.

Trytrim() before passing the data to theempty() function, Something like this:

<?php $item_name=trim($row->item_name); ?>
<input type="text" required="required" name="i_name" class="form-control"
   placeholder="Masukkan nama item..." <?php if (!empty($item_name)) {
echo 'value="' . $row->item_name . '"';
} ?> />

Also, if it was me, I recommend using a cleaner code:

<?php
$item_name=trim($row->item_name);
$value='';
if(!empty($item_name)){
    $value='value="'.$row->item_name.'"';
}
?>
<input type="text" required="required" name="i_name" class="form-control" placeholder="Masukkan nama item..." <?=$value;?> />
229

Answer

Solution:

this might work

<input type="text" required="required" name="i_name" class="form-control"
   placeholder="Masukkan nama item..." <?php if (!empty(trim($row->item_name))) {
echo 'value="' . $row->item_name . '"';
} ?> />

what I have done is changed your

<?php if(!empty(trim($row->item_name))){ ?> value="<?= $row->item_name ?>"<?php } ?>

to

<?php if (!empty(trim($row->item_name))) {
echo 'value="' . $row->item_name . '"';
} ?>
544

Answer

Solution:

Try another approch:

<?php if(!empty(trim($row->item_name))){$i_name = $row->item_name;} ?>
<input type="text" required="required" name="i_name" class="form-control" placeholder="Masukkan nama item..." value="<?php echo $i_name; ?>" />
39

Answer

Solution:

Simple Assign the value to new variablevalue="<?= !empty($ne_variable=trim($row->item_name))? $row->item_name:'' ?>"

<input type="text" required="required" name="i_name" class="form-control" placeholder="Masukkan nama item..." value="<?= !empty($ne_variable=trim($row->item_name))?  $row->item_name:'' ?>" />

People are also looking for solutions to the problem: php - Session Doesnt work first time (want explanation )

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.