Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- echo "My variable value is: " . $my_variable_name;
- echo "My index value is: " . $my_array["my_index"];
- $o = [];
- $var = ["",0,null,1,2,3,$foo,$o['myIndex']];
- array_walk($var,function($v){
- if(!isset($v) || $v == false) {
- echo "emptyn";
- }
- if(empty($v)) {
- echo "emptyn";
- }
- });
- //Initializing variable
- $value = ""; //Initialization value; Examples
- //"" When you want to append stuff later
- //0 When you want to add numbers later
- //isset()
- $value = isset($_POST['value']) ? $_POST['value'] : '';
- //empty()
- $value = !empty($_POST['value']) ? $_POST['value'] : '';
- set_error_handler('myHandlerForMinorErrors', E_NOTICE | E_STRICT)
- error_reporting( error_reporting() & ~E_NOTICE )
- //isset()
- $value = isset($array['my_index']) ? $array['my_index'] : '';
- //array_key_exists()
- $value = array_key_exists('my_index', $array) ? $array['my_index'] : '';
- list($a, $b) = array(0 => 'a');
- //or
- list($one, $two) = explode(',', 'test string');
- // recommended solution
- $user_name = $_SESSION['user_name'];
- if (empty($user_name)) $user_name = '';
- OR
- // just define at the top of the script index.php
- $user_name = '';
- $user_name = $_SESSION['user_name'];
- OR
- $user_name = $_SESSION['user_name'];
- if (!isset($user_name)) $user_name = '';
- // not the best solution, but works
- // in your php setting use, it helps hiding site wide notices
- error_reporting(E_ALL ^ E_NOTICE);
- function ifexists($varname)
- {
- return(isset($$varname)?$varname:null);
- }
- <?=ifexists('name')?>
- function ifexistsidx($var,$index)
- {
- return(isset($var[$index])?$var[$index]:null);
- }
- <?=ifexistsidx($_REQUEST,'name')?>
- $a=10;
- if($a==5) { $user_location='Paris';} else { }
- echo $user_location;
- $a=10;
- if($a==5) { $user_location='Paris';} else { $user_location='SOMETHING OR BLANK'; }
- echo $user_location;
- $value = filter_input(INPUT_POST, 'value');
- if (!isset($_POST['value'])) {
- $value = null;
- } elseif (is_array($_POST['value'])) {
- $value = false;
- } else {
- $value = $_POST['value'];
- }
- $value = (string)filter_input(INPUT_POST, 'value');
- // Echo whatever the hell this is
- <?=$_POST['something']?>
- // If this is set, echo a filtered version
- <?=isset($_POST['something']) ? html($_POST['something']) : ''?>
- $user_location = null;
- $greeting = "Hello, ".exst($user_name, 'Visitor')." from ".exst($user_location);
- /**
- * Function exst() - Checks if the variable has been set
- * (copy/paste it in any place of your code)
- *
- * If the variable is set and not empty returns the variable (no transformation)
- * If the variable is not set or empty, returns the $default value
- *
- * @param mixed $var
- * @param mixed $default
- *
- * @return mixed
- */
- function exst( & $var, $default = "")
- {
- $t = "";
- if ( !isset($var) || !$var ) {
- if (isset($default) && $default != "") $t = $default;
- }
- else {
- $t = $var;
- }
- if (is_string($t)) $t = trim($t);
- return $t;
- }
- <?php
- error_reporting(E_ALL); // making sure all notices are on
- function idxVal(&$var, $default = null) {
- return empty($var) ? $var = $default : $var;
- }
- echo idxVal($arr['test']); // returns null without any notice
- echo idxVal($arr['hey ho'], 'yo'); // returns yo and assigns it to array index, nice
- ?>
- ; Common Values:
- ; E_ALL (Show all errors, warnings and notices including coding standards.)
- ; E_ALL & ~E_NOTICE (Show all errors, except for notices)
- ; E_ALL & ~E_NOTICE & ~E_STRICT (Show all errors, except for notices and coding standards warnings.)
- ; E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR (Show only errors)
- ; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
- ; Development Value: E_ALL
- ; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
- ; http://php.net/error-reporting
- error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
- function foo()
- {
- $my_variable_name = '';
- //....
- if ($my_variable_name) {
- // perform some logic
- }
- }
- // verbose way - generally better
- if (isset($my_array['my_index'])) {
- echo "My index value is: " . $my_array['my_index'];
- }
- // non-verbose ternary example - I use this sometimes for small rules.
- $my_index_val = isset($my_array['my_index'])?$my_array['my_index']:'(undefined)';
- echo "My index value is: " . $my_index_val;
- $my_array = array(
- 'my_index' => ''
- );
- //...
- $my_array['my_index'] = 'new string';
- echo "My variable value is: " . (isset($my_variable_name)) ? $my_variable_name : '';
- echo "My index value is: " . (isset($my_array["my_index"])) ? $my_array["my_index"] : '';
- echo "My variable value is: " . (isset($my_variable_name) && !empty($my_variable_name)) ? $my_variable_name : '';
- echo "My index value is: " . (isset($my_array["my_index"]) && !empty($my_array["my_index"])) ? $my_array["my_index"] : '';
Add Comment
Please, Sign In to add comment