PHP: short cut for isset and! Empty?

I wonder if there are better ideas for solving the problem below,

I have a form with a number of input fields, for example,

<input name="pg_title" type="text" value="" /> <input name="pg_subtitle" type="text" value="" /> <input name="pg_description" type="text" value="" /> <input name="pg_backdate" type="text" value="" /> etc 

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

 <input name="pg_title" type="text" value="" /> etc 

And I have another php page for processing $_POST data,

 $pg_title = null; $pg_subtitle = null; $pg_description = null; $pg_backdate = null; if(isset($_POST['pg_title']) && !empty($_POST['pg_title']) ) $pg_title = $_POST['pg_title']; if(isset($_POST['pg_subtitle']) && !empty($_POST['pg_subtitle']) ) $pg_subtitle = $_POST['pg_subtitle']; if(isset($_POST['pg_description']) && !empty($_POST['pg_description']) ) $pg_description = $_POST['pg_description']; if(isset($_POST['pg_backdate']) && !empty($_POST['pg_backdate']) ) $pg_backdate = $_POST['pg_backdate']; 

Every time I need to check if $_POST set to a specific input field and not empty , otherwise its variable will be set to null , so I will not enter empty space in my db.

I find that isset and !empty in the if condition are very repeated when I have a long list of variables to be processed.

Is there a default php function for the < cut "process above? Or do I need to write a user definition function to handle this?

Or maybe there is another way to do this?

Thanks.

EDIT:

Thank you very much for your help.

Just some kind of extra code on my php page that processes $ _POST data,

 $sql = " UPDATE root_pages SET pg_url = ?, pg_title = ?, pg_subtitle = ?, pg_backdate = ?, pg_description = ?, ... updated_by = ? WHERE pg_id = ? "; $result = $connection->run_query($sql,array( $pg_url, $pg_title, $pg_subtitle, $pg_backdate, $pg_description, ... $pg_id )); 

since you see that $pg_subtitle, $pg_backdate, $pg_description, etc always present in my request. 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 this column.

Thanks: -)

+6
function php
source share
10 answers

You can use a simple function

 function post_value_or($key, $default = NULL) { return isset($_POST[$key]) && !empty($_POST[$key]) ? $_POST[$key] : $default; } 

Then use:

 $pg_title = post_value_or('pg_title'); // OR with a default $pg_title = post_value_or('pg_title', 'No Title'); 
+5
source share

isset && !empty is redundant. The empty language construct is basically abbreviated for !isset($foo) || !$foo !isset($foo) || !$foo , and !empty equivalent to isset($foo) && $foo . This way you can shorten the code by leaving isset checking.

Much simpler:

 $values = array('pg_title' => null, 'pg_subtitle' => null, …); $values = array_merge($values, $_POST); // use $values['pg_title'] etc. 

If you don't want your default null values ​​to be overwritten with false values, for example. '' , you can do something like this:

 $values = array_merge($values, array_filter($_POST)); 

Just remember that '0' also false.

+20
source share

empty($var) is an abbreviation !( isset($var) && $var ) .

So !empty($_POST['...']) will be enough for your situation - calling isset , which you currently have is redundant.

+5
source share

Custom function, I'm afraid. But they are quite short. I have someone lying somewhere if you want to take a look, but this is really trivial, as you can imagine.

Update:

Here I found:

 define('PARAM_INT', 0); define('PARAM_STR', 1); function get_param($name, $default = null, $type = PARAM_INT) { $value = $default; if (isset($_POST[$name])) { $value = $_POST[$name]; } else if (isset($_GET[$name])) { $value = $_GET[$name]; } switch($type) { case PARAM_INT: $value = (int)$value; break; case PARAM_STR: break; default: // error your heart out here } return $value; } 

Of course, now all the cool kids do it with filter_var , but the idea is the same.

+2
source share

+1 for array_merge() , but I think that nonetheless a short form for:

 if (isset($_POST['some_var']) and !empty($_POST['some_var'])) $some_var = $_POST['some_var']; else $some_var = NULL; 

it should be:

 $some_var = $_POST['some_var'] ? $_POST['some_var'] : NULL; 

yes, it triggers a "undefined index" notification, but it checks for both presence and emptiness

EDIT: and naturally returns NULL, as the OP asked.

During a little research, I found an interesting "flow control function" for this case that I had never used before: NULLIF ()

So you can complete this task without PHP. Just wrap all the variables in it:

 NULLIF('".$_REQUEST['some_var']."', '') 

in your request instead of '".$_REQUEST['some_var']."'

If the variable is empty or does not exist, it will be NULLIF('', '') to '' == '' , it will return NULL. Otherwise, it will return the first arg == your variable.

+1
source share

Consider using the advanced filter_input filter. You will avoid the missing Notification index and receive data sanitation at the same time.

0
source share

I always make myself unloved. But the best approach is to overcome the mantra of micro optimization and use the syntax construct that was developed for this @ .

In fact, I'm lying. I use isset() myself too often. (But at least I know it's not very bright.) And for new projects, I now use object-oriented superglobals that combine filtering and implicit isset tests into wrappers $ _POST, $ _GET, $ _REQUEST, $_REQUEST->ascii["title"] or $_GET["raw"] no longer display debugging messages.

0
source share

This function checks if a variable is set, is not empty, after all, if it has any value.

 /** * @param var - testing variable * @param value * @return boolean */ function is(&$var, $value = null){ if(!is_null($value)){ return IsSet($var) && $var == $value; } return IsSet($var) && !empty($var); } echo $_GET['id']; // this produce Warning echo is($_GET['id'])?'1':'0'; // return false echo $_GET['id']; // after first using function is(), will not be produce Warning!!! is($_GET['id']); // return false IsSet($_GET['id']); // return false $_GET['id'] = 7; is($_GET['id'], 7); // return true; 
0
source share

I do not have enough comments for comments. However, the assumption that Vladkrasras used:

 $some_var = $_POST['some_var'] ? $_POST['some_var'] : NULL; 

does not match E_ALL. You should check array keys before accessing them using either empty () or isset (), as others have suggested. Especially for user input.

In addition, his second suggestion is to use the MySQL function "NULLIF ()" as follows:

 NULLIF('".$_REQUEST['some_var']."', '') 

even worse. The insertion of unauthorized user input directly into the SQL query is the primary vector of the SQL injection attack.

0
source share

This is an interesting question, and I am waiting for answers.

However, a little trick you can do inline:

 $pg_title = isset($_POST['pg_title']) && !empty($_POST['pg_title']) ? $_POST['pg_title'] : null; 
-one
source share

All Articles