Skip to content

Instantly share code, notes, and snippets.

@dave-jay
Last active December 16, 2015 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dave-jay/5450379 to your computer and use it in GitHub Desktop.
Save dave-jay/5450379 to your computer and use it in GitHub Desktop.
Escape in php to stop throwing 'undefined index/var' notice while using var directly
<?php
function _e(&$a,$return=NULL){
return isset($a) ? $a : $return;
}
// usages
# scenario #1
$field = _e($_GET['field']);
# if field is not set, it will be set to null
# scenario #2
$field = _e($_GET['field'],'id);
# if field is not set, it will set to 'id'
# saving two lines of if code :)
@dave-jay
Copy link
Author

_e function expects first arg as 'reference', so it wont throw notice :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment