Skip to content
Advertisement

PHP: a short cut for isset and !empty?

I wonder if there any better ideas to solve the problem below,

I have a form with a number of input fields, such as,

JavaScript

But sometimes don’t need certain input fields above in my form, for instance, I only need the page title for my db injection,

JavaScript

And I have another php page to handle the $_POST data,

JavaScript

Every time I will have to check if the $_POST of a certain input field is set and not empty, otherwise its variable will be set to null, so that I won’t inject an empty space into my DB.

I find the isset and !empty in the if-condition are very repetitive when I have a long list of variables to handle.

Is there any default PHP function to ‘shorten‘ the process above? Or do I have to write a user-defined function to handle this?

Or maybe there is another way to do this?

Just some extra code in my php page that handle the $_POST data,

JavaScript

as you see that $pg_subtitle, $pg_backdate, $pg_description, etc always present in my query. so if I get $pg_subtitle = '' instead of $pg_subtitle = null when there is no data in it, my db record will have an empty space for that column.

Advertisement

Answer

You can use a simple function

JavaScript

Then use:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement