Maartin Posted June 12, 2012 Share Posted June 12, 2012 <?php $a = array(); // My Error message: // Notice: Use of undefined constant test - assumed 'test' in K:\www\a.php on line 7 $a[test] = 1; echo $a[test]; ?> // ------------------------------------------------------------ I got php 5.313 $a[test] = 1; // I can't add $a[test] in array anymore! Why this stop working and how can I make php add more array elements? thanks /Martin PS: This code work on my Webhotel that use same PHP 5.3.13 My question is why dosen't it work in my WAMP Installation? Quote Link to comment Share on other sites More sharing options...
btherl Posted June 12, 2012 Share Posted June 12, 2012 Try this: $a['test'] = 1; Quote Link to comment Share on other sites More sharing options...
Maartin Posted June 12, 2012 Author Share Posted June 12, 2012 thanks! It look like I am forced to $a['test'] can I unforce it so I only need to write $a[test] somewhere in php.ini? Quote Link to comment Share on other sites More sharing options...
requinix Posted June 12, 2012 Share Posted June 12, 2012 No. And you shouldn't, because using quotes is what you should be doing. Array keys are integers or strings, and with $a[test] = 1; PHP tries to find a constant "test". Even if it existed that's not what you'd want it to use. Strings have quotes, so if you want a string then use quotes. Quote Link to comment Share on other sites More sharing options...
Maartin Posted June 12, 2012 Author Share Posted June 12, 2012 I been $a['name'] the whole day now... I try use display_errors = On in php.in I still get: Notice: Undefined index: a in ... if($a['test'] == "b") ...; // when !isset($a['test']) I get the Notice!; Can I remove Notice when php.ini: display_errors = On Quote Link to comment Share on other sites More sharing options...
trq Posted June 12, 2012 Share Posted June 12, 2012 The code you have showed us will not produce the error you have shown us. Fix your code. If you can't, post the problematic code. Turning error reporting off because you have errors is the sign of a poor programmer. Quote Link to comment Share on other sites More sharing options...
Maartin Posted June 12, 2012 Author Share Posted June 12, 2012 if($a['test'] == 'b') if test is unset I get a Notice like this: Notice: Use of undefined constant test - assumed 'test' in K:\www\a.php on line 7 if I turn off display_errors = Off in php.in the page run FINE with ignorant bliss of all the notices! I continue change all the [name] to ['name'] and is thankful for my new wisdom! My wish was to show only ERRORS not all Notices! Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 12, 2012 Share Posted June 12, 2012 Again, that error will not be produced by that code. You are likely missing the single quotes somewhere. You can set error reporting to a specific level, but you really should find and fix the notices. Quote Link to comment Share on other sites More sharing options...
Maartin Posted June 12, 2012 Author Share Posted June 12, 2012 This is the code in the picture: <?php // PHP 5.3.13 // $a = array(); // Error // $a = array('test' => 'q'); // OK no Notice if($a['test'] != 'b') echo "hello"; ?> Thanks for telling me: "The code you have showed us will not produce the error you have shown us. Fix your code. If you can't, post the problematic code. Turning error reporting off because you have errors is the sign of a poor programmer." but you wrong thorpe the code generate this Notice even if you like insult me! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 12, 2012 Share Posted June 12, 2012 This is the code in the picture: <?php // PHP 5.3.13 // $a = array(); // Error // $a = array('test' => 'q'); // OK no Notice if($a['test'] != 'b') echo "hello"; ?> Thanks for telling me: "The code you have showed us will not produce the error you have shown us. Fix your code. If you can't, post the problematic code. Turning error reporting off because you have errors is the sign of a poor programmer." but you wrong thorpe the code generate this Notice even if you like insult me! But he's absolutely right. You should fix errors, not just turn off error reporting/logging. Turning off error reporting an ignoring errors is just plain lazy, and will cause you major headaches in the future. And if you notice, the error message is undefined variable: a, not undefined index: a, as you previously said it was. There's a pretty big difference between the two. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 12, 2012 Share Posted June 12, 2012 Yeah it helps if you don't comment out the definition of a variable. You are trying to use $a before it's defined. $a['test'] isn't defined there either. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.