开发者

PHP Array key strings without quotation marks

开发者 https://www.devze.com 2023-04-04 21:50 出处:网络
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..开发者_如何学JAVA

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..开发者_如何学JAVA 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消