I’m using CodeIgniter and i am using the following snippet is part of an edit form. What it does is to take a value to edit, and populate the field. The same form-builder is used to rebuild the form when there are validation errors, and the fields will be repopulated values that may or may not have been edited by the user.
$field = form_input(array(
'name' => 'title',
'id' => 'title'
), set_value('title') || 'givenValue') <-- this part
in CodeIgniter, .set_value() just returns a string that was a value of some form field if the form had an error (basically used for repopulating the fields after an error).
i know that in JavaScript i can do like var someObj = param.obj || {} which will result into someObj equals param.obj if it exists or equals to {} otherwise. In PHP snippet above, it returns 1 (boolean i suppose).
In summary, I want to have the former value if it exists, or if not, use a predetermined value (additionally, a blank value if it’s the new form)
How do I do this “inline evaluation” in PHP? or is there a better way to do this in CodeIgniter?
Update
According to the manual, CI already provides a default option for you
In pure PHP, if you’re using PHP 5.3, you can use the ternary shortcut
otherwise, try the long version