{"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[pastacode lang=\u201djavascript\u201d manual=\u201d%24o%20%3D%20%5B%5D%3B%0A%24var%20%3D%20%5B%22%22%2C0%2Cnull%2C1%2C2%2C3%2C%24foo%2C%24o%5B\u2019myIndex\u2019%5D%5D%3B%0Aarray_walk(%24var%2Cfunction(%24v)%0A%7B%0Aif(!isset(%24v)%20%7C%7C%20%24v%20%3D%3D%20false)%20%0A%7B%0Aecho%20%22empty%5Cn%22%3B%0A%7D%0Aif(empty(%24v))%20%0A%7B%0Aecho%20%22empty%5Cn%22%3B%0A%7D%0A%7D)%3B%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201djavascript\u201d manual=\u201d%2F%2FInitializing%20variable%0A%24value%20%3D%20%22%22%3B%20%2F%2FInitialization%20value%3B%20Examples%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%22%22%20When%20you%20want%20to%20append%20stuff%20later%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F0%20%20When%20you%20want%20to%20add%20numbers%20later%0A%2F%2Fisset()%0A%24value%20%3D%20isset(%24_POST%5B\u2019value\u2019%5D)%20%3F%20%24_POST%5B\u2019value\u2019%5D%20%3A%20\u201d%3B%0A%2F%2Fempty()%0A%24value%20%3D%20!empty(%24_POST%5B\u2019value\u2019%5D)%20%3F%20%24_POST%5B\u2019value\u2019%5D%20%3A%20\u201d%3B%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dphp\u201d manual=\u201dset_error_handler(\u2018myHandlerForMinorErrors\u2019%2C%20E_NOTICE%20%7C%20E_STRICT)%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dphp\u201d manual=\u201derror_reporting(%20error_reporting()%20%26%20~E_NOTICE%20)%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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 -> php->php.ini<\/strong><\/li>\n<li>Open up <strong>php.ini -> search for error_reporting = E_ALL and replace this with error_reporting = E_ALL & ~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[pastacode lang=\u201djavascript\u201d manual=\u201d%24data%20%3D%20array(\u2018foo\u2019%20%3D%3E%20\u201942\u2019%2C%20\u2019bar\u2019)%3B%0Aecho%20%24data%5B\u2019spinach\u2019%5D%3B%0Aecho%20%24data%5B1%5D%3B%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201djavascript\u201d manual=\u201d%24data%20%3D%20array(\u2018foo\u2019%20%3D%3E%20\u201942\u2019%2C%20\u2019bar\u2019)%3B%0Aif%20(array_key_exists(\u2018spinach\u2019%2C%20%24data))%20%0A%7B%0A%20%20%20%20echo%20%24data%5B\u2019spinach\u2019%5D%3B%0A%7D%0Aelse%20%0A%7B%0A%20%20%20%20echo%20\u2019No%20key%20spinach%20in%20array\u2019%3B%0A%7D%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>If you have code like:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%20echo%20%24_POST%5B\u2019message\u2019%5D%3B%20%3F%3E%0A%3Cform%20method%3D%22post%22%20action%3D%22%22%3E%0A%20%20%20%20%3Cinput%20type%3D%22text%22%20name%3D%22message%22%3E%0A%20%20%20%20\u2026%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>then <b>$_POST[\u2018message\u2019] <\/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[pastacode lang=\u201djavascript\u201d manual=\u201dif%20(%24_POST)%20%20..%20%20%2F%2F%20if%20the%20%24_POST%20array%20is%20not%20empty%0A%2F%2F%20or%0Aif%20(%24_SERVER%5B\u2019REQUEST_METHOD\u2019%5D%20%3D%3D%20\u2019POST\u2019)%20..%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201djavascript\u201d manual=\u201d%2F%2Fisset()%0A%24value%20%3D%20isset(%24array%5B\u2019my_index\u2019%5D)%20%3F%20%24array%5B\u2019my_index\u2019%5D%20%3A%20\u201d%3B%0A%2F%2Farray_key_exists()%0A%24value%20%3D%20array_key_exists(\u2018my_index\u2019%2C%20%24array)%20%3F%20%24array%5B\u2019my_index\u2019%5D%20%3A%20\u201d%3B%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201djavascript\u201d manual=\u201dlist(%24a%2C%20%24b)%20%3D%20array(0%20%3D%3E%20\u2019a\u2019)%3B%0A%2F%2For%0Alist(%24one%2C%20%24two)%20%3D%20explode(\u2018%2C\u2019%2C%20\u2019test%20string\u2019)%3B%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]\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}]}}