3

I am moving the files to the server and is using variables like $_GET[mode] without ''(single quotes) in 'mode'. It works perfectly locally but on the server i am getting notices.. How can i overcome this?here is my phpinfo file phpinfo Is there any way we can have same behavior to work on server as well?

Community
  • 1
  • 1
June
  • 793
  • 2
  • 17
  • 43
  • 3
    Use single quotes. It doesn't work "perfectly", you just ignore PHP when it's telling you "hey, you shouldn't be doing this!". – Jon Sep 15 '11 at 13:26
  • Why would you want to do that? It is bad practice and causes issues like you have seen... – Marc Towler Sep 15 '11 at 13:26
  • i know i can use E_NOTICE but i donot want to use that ...it would be alot of trouble to move everything – June Sep 15 '11 at 13:29
  • Also can i use variables like $_LANG in php? – June Sep 15 '11 at 13:29
  • 1
    @June: Come on. One question at a time. – Lightness Races in Orbit Sep 15 '11 at 13:30
  • 2
    @June It may be a lot of trouble but your existing code is incorrect. Either you fix the problems, or you get error messages. Your choice. And yes, you can name use/name variables like `$_LANG` or whatever you choose, but a) this is not recommended in case a future version PHP decides to define those variables and it may break you code, and b) they will not be superglobals - (annoyingly) there is no way to define a custom superglobal in PHP. – DaveRandom Sep 15 '11 at 13:37

2 Answers2

10

How can you overcome the E_NOTICEs that complain that you forgot quotes around your strings?

Add quotes around your strings.

$_GET['mode']

Also it sounds like the error_reporting level on your local server is not sufficient. It should be high, so that you see these sorts of mistakes as you develop your website/application.

On your production server it may be set lower.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
7

Quoting the PHP Manual on Arrays:

Why is $foo[bar] wrong?

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not. But why? It is common to encounter this kind of syntax in old scripts:

<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>

This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

So, in other words: mode is an undefined constant.

Quoting the PHP Manual on Constants:

If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a string (CONSTANT vs "CONSTANT"). An error of level E_NOTICE will be issued when this happens. See also the manual entry on why $foo[bar] is wrong (unless you first define() bar as a constant). If you simply want to check if a constant is set, use the defined() function.

Solution: put quotes around the key.

Gordon
  • 312,688
  • 75
  • 539
  • 559