{"id":18472,"date":"2022-10-31T23:47:05","date_gmt":"2022-10-31T18:17:05","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/"},"modified":"2022-10-31T23:47:05","modified_gmt":"2022-10-31T18:17:05","slug":"solved-php-ifvar-used-to-work-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/","title":{"rendered":"[Solved] Php if($var) used to work [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-26234215\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"26234215\" data-parentid=\"26234036\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You didn&#8217;t get the error before, because your error_reporting and\/or display_error settings were set too forgiving. Your first snippet <em>is<\/em> attempting to access a value in an array that might not exist. If it doesn&#8217;t PHP <strong><em>always<\/em><\/strong> issues a notice.<br \/>\nIt&#8217;s only <em>after<\/em> the notice has been issued that it can be hushed up (by turning off <code>display_errors<\/code> or setting <code>error_reporting<\/code> to not report notices (<code>~E_NOTICE<\/code>).<\/p>\n<p>You&#8217;re asking for a quick fix: <\/p>\n<pre><code>if (isset($_GET['something']))\n<\/code><\/pre>\n<p>only requires you to add <code>isset()<\/code>. That&#8217;s quick, and it fixes the issue. Besides: using <code>isset<\/code> is what you <em>should<\/em> do. Always.<\/p>\n<hr>\n<p><strong><em>Off-topic, but perhaps useful:<\/em><\/strong><br \/>\nAs I&#8217;ve explained in the comments: the best course of action for you now is to fix the issue itself, not ignoring the notices. To that end, a simple script that scans your projects directories for .php files (using <code>glob<\/code>), reads them in and looks for a pattern might prove useful:<\/p>\n<pre><code>foreach ($phpFiles as $file)\n{\n    $code = file_get_contents($file);\/\/read the file\n    if (preg_match_all('\/if\\s*\\(\\$[^[]+\\[[^]]+\\]\\s*\\)\/',$code, $matches))\n    {\/\/find all usages of \"if ($rawVar['offset'])\" in code\n        echo 'Expressions to check or fix in file: ', $file,':', PHP_EOL, '=&gt;';\n        echo implode(PHP_EOL.'=&gt;', $matches[0]), PHP_EOL;\n    }\n}\n<\/code><\/pre>\n<p>run the script, perhaps write the output to a temp file, and set to work. The output should look something like:<\/p>\n<pre><code>Expressions to check or fix in file: scriptname.php:\n=&gt; if ($_GET['something'])\n=&gt; if ($var[123])\n<\/code><\/pre>\n<p>And so on. It&#8217;s a workable format, and given time, you might be able to write a script for you to automatically refactor the code, too.<br \/>\nPerhaps a more comprehensive (as in complete) regex to use here would be something like:<\/p>\n<pre><code>\/if\\s*\\((\\|\\||&amp;&amp;|\\()?\\$[^[]+\\[[^]]+\\]\\s*(\\|\\||&amp;&amp;|\\))?\/\n<\/code><\/pre>\n<p>But this, too, has some caveats still, but it&#8217;s a start.<\/p>\n<hr>\n<p>Adding assignments &#8211; I&#8217;ll do you one more favour: adding code that fixes the issues you get in assignment expressions like <code>$var = $_GET['something'];<\/code>. This can be done automatically, and quite easily, too:<\/p>\n<pre><code>foreach ($phpFiles as $file)\n{\n    $code = file_get_contents($file);\/\/read the file\n    $clean = preg_replace(\n        '\/(\\$[^\\s]+\\s*={1}\\s*)(\\$[^[]+[^]]+\\])\\s*;\/',\n        '$1isset($2) ? $2 : null;',\n        $code\n    );\n    file_put_contents($file, $clean);\n}\n<\/code><\/pre>\n<p>I&#8217;ve tested this code like so:<\/p>\n<pre><code>$code=\"$foo = 123;\n$foo = $_GET[\"bar\"];\";\n$clean = preg_replace(\n    '\/(\\$[^\\s]+\\s*={1}\\s*)(\\$[^[]+[^]]+\\])\\s*;\/',\n    '$1isset($2) ? $2 : null;',\n    $code\n);\necho $clean, PHP_EOL;\n<\/code><\/pre>\n<p>And it yielded the expected output:<\/p>\n<pre><code>$foo = 123;\n$foo = isset($_GET[\"bar\"]) ? $_GET[\"bar\"] : null;\n<\/code><\/pre>\n<p>Combine these two regex&#8217;s and you should be well on your way to refactoring the code you have&#8230;<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">9<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Php if($var) used to work [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You didn&#8217;t get the error before, because your error_reporting and\/or display_error settings were set too forgiving. Your first snippet is attempting to access a value in an array that might not exist. If it doesn&#8217;t PHP always issues a notice. It&#8217;s only after the notice has been issued that it can be hushed up &#8230; <a title=\"[Solved] Php if($var) used to work [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/\" aria-label=\"More on [Solved] Php if($var) used to work [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[639,4469,339],"class_list":["post-18472","post","type-post","status-publish","format-standard","hentry","category-solved","tag-if-statement","tag-isset","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Php if($var) used to work [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Php if($var) used to work [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You didn&#8217;t get the error before, because your error_reporting and\/or display_error settings were set too forgiving. Your first snippet is attempting to access a value in an array that might not exist. If it doesn&#8217;t PHP always issues a notice. It&#8217;s only after the notice has been issued that it can be hushed up ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-31T18:17:05+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Php if($var) used to work [closed]\",\"datePublished\":\"2022-10-31T18:17:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/\"},\"wordCount\":308,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"if-statement\",\"isset\",\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/\",\"name\":\"[Solved] Php if($var) used to work [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-31T18:17:05+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Php if($var) used to work [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Php if($var) used to work [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Php if($var) used to work [closed] - JassWeb","og_description":"[ad_1] You didn&#8217;t get the error before, because your error_reporting and\/or display_error settings were set too forgiving. Your first snippet is attempting to access a value in an array that might not exist. If it doesn&#8217;t PHP always issues a notice. It&#8217;s only after the notice has been issued that it can be hushed up ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/","og_site_name":"JassWeb","article_published_time":"2022-10-31T18:17:05+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Php if($var) used to work [closed]","datePublished":"2022-10-31T18:17:05+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/"},"wordCount":308,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["if-statement","isset","php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/","name":"[Solved] Php if($var) used to work [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-31T18:17:05+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-php-ifvar-used-to-work-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Php if($var) used to work [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=18472"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18472\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=18472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=18472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=18472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}