CodeIgniter Forums

Full Version: Can someone "translate" this in plain English to me, please?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]developer10[/eluser]
Code:
$update['id'] = (!empty($_POST['id'])) ? $this->input->post('id') : null;

i suppose this line allows the id field to be null. however i need it never to be null. if i remove ": null", it throws
the "unexpected ;" error

El Forum

[eluser]Craig A Rodway[/eluser]
It uses the ternary operator to set the value of $update['id'] depending on the return value of testing if $_POST['id'] is empty or not.

If $_POST['id'] is NOT empty, then $update['id'] is set to the POST'd ID; otherwise it is set to null. Think of it this way:

Code:
$result = (condition) ? IF_TRUE : IF_FALSE;

If you want to remove it, just do this:

Code:
$update['id'] = $this->input->post('id');

However:
Quote:The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist.

Input Class

El Forum

[eluser]Blaze Boy[/eluser]
[quote author="cold_fusion" date="1262495063"]
Code:
$update['id'] = (!empty($_POST['id'])) ? $this->input->post('id') : null;

i suppose this line allows the id field to be null. however i need it never to be null. if i remove ": null", it throws
the "unexpected ;" error[/quote]

this line will put $update['id'] with null if it's not sent with post ...
the idea is to show error of it's not sent
if( $this->input->post('id') ) $update['id'] = $this->input->post('id');
else show_error('error: id must be set');

El Forum

[eluser]developer10[/eluser]
thanks, i'm about to elaborate on this,

btw, i was trying to remember where in the user guide i saw this, but to no avail Smile

Thanks again!

El Forum

[eluser]Colin Williams[/eluser]
Well, the CI user guide covers CodeIgniter, not the entirety of PHP.