PHP error - Empty() function

Asked

Viewed 242 times

7

I have the following mistake:

Fatal error: Can't use method return value in write context in /home/username/public_html/Administrar/application/controllers/holerites.php on line 29

Follow the sequence of rows 29 below:

if(empty($this->input->post('periodo'))) $periodo = date("m")+1; else $periodo = $this->input->post('periodo');
if(empty($this->input->post('dataInicial'))) $data_inicial = ""; else $data_inicial = $this->input->post('dataInicial');
if(empty($this->input->post('dataFinal'))) $data_final = ""; else $data_final = $this->input->post('dataFinal');

I have no idea why the localhost works but the server does not.

3 answers

6


empty is not a function, is part of the language (also called "language constructor").

You cannot call on top of a value or a function or method return because it always expects a reference (variable).

This is the behavior expected in previous versions of php 5.5.

Example in several versions of php

  • I added what I needed to a variable, and I was able to verify it. $periodo = $this->input->post('periodo');

5

At Rray’s suggestion, I did it this way:

$periodo        = $this->input->post('periodo');
$dataInicial    = $this->input->post('dataInicial');
$dataFinal      = $this->input->post('dataFinal');

if(empty($periodo)) $periodo = date("m"); else $periodo = $this->input->post('periodo');
if(empty($dataInicial)) $data_inicial = ""; else $data_inicial = $this->input->post('dataInicial');
if(empty($dataFinal)) $data_final = ""; else $data_final = $this->input->post('dataFinal');

2

As @rray put it, this "language builder", the empty, does not work with expressions in versions prior to PHP 5.5.

My suggestion to make the code simple would be this:

if (! ($periodo = $this->input->post('periodo'))) {

   $periodo = date("m");
}

echo $periodo;

Or else:

 $periodo = $this->input->post('periodo') ?: date('m');

Browser other questions tagged

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