{"id":955,"date":"2017-03-20T10:44:34","date_gmt":"2017-03-20T05:14:34","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=955"},"modified":"2018-10-30T12:19:33","modified_gmt":"2018-10-30T06:49:33","slug":"php-notice-undefined-variable-notice-undefined-index-notice-undefined-offset","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/php-notice-undefined-variable-notice-undefined-index-notice-undefined-offset\/","title":{"rendered":"\u201cNotice: Undefined variable\u201d, \u201cNotice: Undefined index\u201d, and \u201cNotice: Undefined offset\u201d"},"content":{"rendered":"<h2 id=\"notice-undefined-variable\"><span style=\"color: #ff6600;\"><b>Notice: Undefined <\/b><b>variable<\/b><\/span><\/h2>\n<ul>\n<li>Default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name.<\/li>\n<\/ul>\n<ul>\n<li>It is also a major <b>security risk <\/b>with register_globals turned on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array.<\/li>\n<\/ul>\n<ul>\n<li><strong>isset()<\/strong> language construct can be used to detect if a variable has been already initialized.<\/li>\n<\/ul>\n<ul>\n<li>Additionally and more ideal is the solution of empty() since it does not generate a warning or error message if the variable is not initialized.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$o = [];<br\/>$var = [&quot;&quot;,0,null,1,2,3,$foo,$o[&#039;myIndex&#039;]];<br\/>array_walk($var,function($v)<br\/>{<br\/>if(!isset($v) || $v == false) <br\/>{<br\/>echo &quot;empty\\n&quot;;<br\/>}<br\/>if(empty($v)) <br\/>{<br\/>echo &quot;empty\\n&quot;;<br\/>}<br\/>});<\/code><\/pre> <\/div>\n<ul>\n<li>Although <a href=\"https:\/\/www.wikitechy.com\/php\/learn-php-online\" target=\"_blank\" rel=\"noopener\">PHP<\/a> does not require variable declaration, it does recommend it in order to avoid some security vulnerabilities or bugs where one would forget to give a value to a variable that he will use later in the script.<\/li>\n<\/ul>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>What PHP does in the case of undeclared variables is issue a very low level error, <b>E_NOTICE<\/b>, one that is not even reported by default, but the Manual advises to allow during development.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4 id=\"ways-to-deal-with-the-issue\"><span style=\"color: #808000;\"><b>Ways to deal with the issue:<\/b><\/span><\/h4>\n<ul>\n<li style=\"list-style-type: none;\">\n<ol>\n<li><b>Recommended:<\/b> Declare your variables, for example when you try to append a string to an undefined variable. Or use <b>isset<\/b><b>() <\/b>\/ <b>!empty() <\/b>to check if they are declared before referencing them, as in:<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">\/\/Initializing variable<br\/>$value = &quot;&quot;; \/\/Initialization value; Examples<br\/>             \/\/&quot;&quot; When you want to append stuff later<br\/>             \/\/0  When you want to add numbers later<br\/>\/\/isset()<br\/>$value = isset($_POST[&#039;value&#039;]) ? $_POST[&#039;value&#039;] : &#039;&#039;;<br\/>\/\/empty()<br\/>$value = !empty($_POST[&#039;value&#039;]) ? $_POST[&#039;value&#039;] : &#039;&#039;;<\/code><\/pre> <\/div>\n<ul>\n<li>Set a\u00a0custom error handler for <strong>E_NOTICE<\/strong> and redirect the messages away from the standard output.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">set_error_handler(&#039;myHandlerForMinorErrors&#039;, E_NOTICE | E_STRICT) <\/code><\/pre> <\/div>\n<ul>\n<li><strong>Disable<\/strong> E_NOTICE from reporting. A quick way to exclude just <b>E_NOTICE<\/b> is:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">error_reporting( error_reporting() &amp; ~E_NOTICE ) <\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"how-to-fix-php-error-notice-undefined-variable-index\"><span style=\"color: #008000;\"><b>How To Fix PHP Error Notice: Undefined variable: index<\/b><\/span><\/h4>\n<p>Below are the steps to fix How To Fix PHP Error Notice: Undefined variable: index<\/p>\n<ol>\n<li>Open up <strong>wamp server -&gt; php-&gt;php.ini<\/strong><\/li>\n<li>Open up <strong>php.ini -&gt; search for error_reporting = E_ALL and replace this with error_reporting = E_ALL &amp; ~E_NOTICE<\/strong><\/li>\n<li><strong>Restart<\/strong> your wamp server<\/li>\n<\/ol>\n<h2 id=\"notice-undefined-index\"><span style=\"color: #ff6600;\"><b>Notice: Undefined Index<\/b><\/span><\/h2>\n<ul>\n<li>Happens when you try to access an <a href=\"https:\/\/www.wikitechy.com\/step-by-step-tutorials\/php\/php-arsort-function\" target=\"_blank\" rel=\"noopener\">array<\/a> by a key that does not exist in the array.<\/li>\n<li>A typical example for an <b>Undefined Index <\/b>notice.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$data = array(&#039;foo&#039; =&gt; &#039;42&#039;, &#039;bar&#039;);<br\/>echo $data[&#039;spinach&#039;];<br\/>echo $data[1];<\/code><\/pre> <\/div>\n<ul>\n<li>Both <b>spinach and 1 <\/b>do not exist in the array, causing an <b>E_NOTICE<\/b> to be triggered.<\/li>\n<li>The solution is to make sure the index or offset exists prior to accessing that index.<\/li>\n<li>This may mean that you need to fix a bug in your program to ensure that those indexes do exist when you expect them to. Or it may mean that you need to test whether the indexes exist using\u00a0<strong>array_key_exists [http:\/\/php.net\/array_key_exists] or\u00a0isset[http:\/\/php.net\/isset]:<\/strong><\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$data = array(&#039;foo&#039; =&gt; &#039;42&#039;, &#039;bar&#039;);<br\/>if (array_key_exists(&#039;spinach&#039;, $data)) <br\/>{<br\/>    echo $data[&#039;spinach&#039;];<br\/>}<br\/>else <br\/>{<br\/>    echo &#039;No key spinach in array&#039;;<br\/>}<\/code><\/pre> <\/div>\n<ul>\n<li>If you have code like:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php echo $_POST[&#039;message&#039;]; ?&gt;<br\/>&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;<br\/>    &lt;input type=&quot;text&quot; name=&quot;message&quot;&gt;<br\/>    ...<\/code><\/pre> <\/div>\n<ul>\n<li>then <b>$_POST[&#8216;message&#8217;] <\/b>will not be set when this page is first loaded and you will get the above error.<\/li>\n<li>Only when the form is submitted and this code is run a second time will the array index exist. You typically check for this with:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">if ($_POST)  ..  \/\/ if the $_POST array is not empty<br\/>\/\/ or<br\/>if ($_SERVER[&#039;REQUEST_METHOD&#039;] == &#039;POST&#039;) ..<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h2 id=\"notice-undefined-index-undefined-offset\"><span style=\"color: #800080;\"><b>Notice: Undefined index \/ Undefined offset<\/b><\/span><\/h2>\n<ul>\n<li>This notice appears when you (or PHP) try to access an undefined index of an array.<\/li>\n<li>Ways to deal with the issue:<\/li>\n<\/ul>\n<p>Check if the index exists before you access it. For this you can use\u00a0<a href=\"https:\/\/www.wikitechy.com\/technology\/difference-isset-array_key_exists\/\" target=\"_blank\" rel=\"noopener\">isset() or\u00a0array_key_exists()<\/a><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">\/\/isset()<br\/>$value = isset($array[&#039;my_index&#039;]) ? $array[&#039;my_index&#039;] : &#039;&#039;;<br\/>\/\/array_key_exists()<br\/>$value = array_key_exists(&#039;my_index&#039;, $array) ? $array[&#039;my_index&#039;] : &#039;&#039;;<\/code><\/pre> <\/div>\n<ul>\n<li>The language construct\u00a0list()may generate this when it attempts to access an array index that does not exist:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">list($a, $b) = array(0 =&gt; &#039;a&#039;);<br\/>\/\/or<br\/>list($one, $two) = explode(&#039;,&#039;, &#039;test string&#039;);<\/code><\/pre> <\/div>\n<ul>\n<li>Two variables are used to access two array elements, however there is only one array element, index <b>0,<\/b> so this will generate:<\/li>\n<li><b>Notice: Undefined offset: 1<\/b><\/li>\n<\/ul>\n<h3 id=\"_post-_get-_session-variable\"><span style=\"color: #003300;\"><b>$_POST \/ $_GET \/ $_SESSION variable:<\/b><\/span><\/h3>\n<ul>\n<li>The notices above appear often when working with\u00a0<a href=\"https:\/\/www.wikitechy.com\/php\/php-post\" target=\"_blank\" rel=\"noopener\">$_POST<\/a>,\u00a0<a href=\"https:\/\/www.wikitechy.com\/php\/php-get\" target=\"_blank\" rel=\"noopener\">$_GET<\/a> or\u00a0<a href=\"https:\/\/www.wikitechy.com\/php\/php-login-form-with-sessions\" target=\"_blank\" rel=\"noopener\">$_SESSION<\/a>. For\u00a0$_POST and $_GET\u00a0you just have to check if the index exists or not before you use them.<\/li>\n<\/ul>\n<ul>\n<li>For\u00a0$_SESSION you have to make sure you have the session started with\u00a0session_start() and that the index also exists.<\/li>\n<\/ul>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Notice: Undefined variable Default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[1809,1800,1812,1808,1818,1803,1807,1814,1810,1813,1816,1811,1815,1817,1806,1804,1802,1805,1801],"class_list":["post-955","post","type-post","status-publish","format-standard","hentry","category-php","tag-_post-not-working-notice-undefined-index-username","tag-how-to-remove-undefined-variable-error-in-php","tag-how-to-solve-undefined-variable-error-in-php","tag-notice-undefined-index-zzzzzzwtf","tag-php-arrays-variables-warnings-undefined-index","tag-php-undefined-variable-but-it-is-defined","tag-undefined-index-error-in-php","tag-undefined-index-error-in-php-post","tag-undefined-index-for-_post-noob-question","tag-undefined-index-php-_post","tag-undefined-index-php-array","tag-undefined-index-php-error-solution","tag-undefined-index-php-get","tag-undefined-index-php-session","tag-undefined-variable-codeigniter","tag-undefined-variable-javascript","tag-undefined-variable-laravel","tag-undefined-variable-matlab","tag-undefined-variable-php-solution"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=955"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/955\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}