PHP Notice: Use of undefined constant var - assumed 'var' in xampp


Posted on : Wed, 15th Apr 15 01:59 pm (Updated on : Wed, 15th Apr 15 01:59 pm) UTC
Author : Code2care
100+ votes

Notice: Use of undefined constant var2 - assumed 'var2' in /Applications/xamppfiles/htdocs/quiz/php/file.php on line 41

I got this Notice while trying to display contents of a JSON object using PHP, though this is not a blocking issue but the notice will be displayed on the browser to the user.

File : file.php Language : PHP

<?php

class myObj {
	
	
	function getJson() {
		
		
		$json = '[
        {
            "var1": "20",
            "var2": "120"
        },
        {
            "q1": "String",
            "qtype": "1",
            "o1": "Option 1",
            "o2": "Option 2",
            "o3": "Option 4",
            "o4": "Option 5",
            "desc": "String"
        },
        {
            "q2": "String",
            "qtype": "1",
            "o1": "Option 1",
            "o2": "Option 2",
            "o3": "Option 4",
            "o4": "Option 5",
            "desc": "String"
        }
    ]';
		
		
		$obj = json_decode($json,true);
		
		var_dump($obj);
		
		
		echo $obj[0]["var1"];
		echo $obj[0][var2]; // Notice at this line because the Quotes are missing!!
	

		
		
	}
	
	
	
	
}


 $obj = new myObj;
 
 $obj ->getJson();
 

?>



Advertisements





What is causing this issue is that i have missed to surround var2 with quotes (either single or double), though PHP is Interpreter is smart enough to resolve this but throws a message.

Code2care's mission is to share varied knowledge in technical and non-technical areas gathered during day-to-day learnings and development activities so that our visitors can leverage this portal to find solutions to their queries without re-inventing the wheel. Technical posts include Learnings, Tutorials, Video Tutorials, Code Snippets, Tips-n-tricks.

Follow us : Facebook - Twitter - Google +