PHP errors for (isset) variables and optimization
My error logs get huge quick because I reference a ton of variables with may or may not exist. I have been going back through and starting to clean them up but wanted to see if there was a more effective (and cleaner) way of checking for php variables.
Currently I use as the following:
Example 1:
<?php
if (isset($_SESSION['checked']) && $_SESSION['checked'] == "Y") {
do something;
}
?>
Example 2: (within html form)
<input type="text" name="sample_number" onKeyUp="this.value=this.value.replace(/[^0-9]/ig, '')" value="<?php echo $fields['sample_开发者_StackOverflow社区number']; ?>" />
where sample number is displayed if it is set.
is there a better way to handle scenarios like these?
It's a little more verbose, but array_key_exists()
is faster than isset()
for checking array key presence, and isset()
comes with the caveat that it will return false for NULL
values (you may have intentionally set a variable to NULL
, but isset()
will be semantically wrong in that case).
So, in your first example:
<?php
if(array_key_exists('checked', $_SESSION) && $_SESSION['checked'] == 'Y') {
do something;
}
?>
In your second example, I suggest using array_key_exists()
and the ternary operator:
<input type="text" name="sample_number" onKeyUp="this.value=this.value.replace(/[^0-9]/ig, '')" value="<?= array_key_exists('sample_number', $fields) ? $fields['sample_number'] : '' ?>" />
as it makes for a more compact solution without repeating your HTML through if()
conditions.
Edit: I always like to initialize my variables at the earliest possible point, FWIW. This can get tricky as there's always the potential overhead if, say, you had a slew of arrays you initialized but then didn't end up using them (eg, if they were only used in certain conditions), but then again everything's relative to your application. There's no substitute for good judgment.
I usually write myself some function that gets me the values out of the related array (being it session, get or post). I usually make these function assign a default value, as I don't really care if a value is set or the default value is set (which leads to no special behaviour).
Essentially, a function could look like this:
function getSessionVar ( $name, $default = '' )
{
if ( !isset( $_SESSION[$name] )
return $default;
switch ( gettype( $default ) )
{
'integer':
return intval( $_SESSION[$name] );
break;
'double':
return floatval( $_SESSION[$name] );
break;
default:
return $_SESSION[$name];
}
}
Then I just use this function at the beginning of a file/code block to get all variables I'm interested in and just work with the value of that variable.
isset is the method that they teach in Zend certification training. You could take shortcuts and do the @ in front of the statement that's barking, or you could just turn the error level down, but I'm a firm believer that errors exist for a reason....well, most of them anyways. Take your time and do it right.
It's not the best way around it, but if you badly need to report all errors but still wanna ignore the case you described up there, I guess you could do:
if ($test = @$_SESSION['checked'] && $test == 'Y') {
do something
}
精彩评论