开发者

Is there something in PHP similar to Option Explicit in VB [duplicate]

开发者 https://www.devze.com 2022-12-20 13:35 出处:网络
This question already has answers here: 开发者_开发问答 Closed 10 years ago. Possible Duplicate: Strict mode in PHP?
This question already has answers here: 开发者_开发问答 Closed 10 years ago.

Possible Duplicate:

Strict mode in PHP?

I am doing a big project in PHP. In PHP you do not need to declare variables. This is causing a lot of problems for me.

In Visual Basic 6, the Option Explicit statement makes it mandatory to declare variables. Is something similar available in PHP?


If you turn on E_NOTICE error messages, PHP will tell you about uninitialized variables:

ini_set("error_reporting", E_ALL);

Uninitialized is a little bit different than undeclared, but it should give you a similar effect.


error_reporting(E_ALL);

throws a notice when you attempt to use an undefined variable

a more general tip: use functions instead of global code, and make them small (max. 20 lines). Since variables are local to functions, there's less chance to forget or misspell a variable name.


Increasing the error reporting level only affects php's behaviour when an undefined variable/element is used as rvalue, like echo $doesnotexist;.
But option explicit on also prohibits the use of undeclared variables as lvalue

Option Explicit On
Dim x As Integer
x = 10
y = 11 ' error, variable is not declared

There's no similar option or config parameter in php.

0

精彩评论

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

关注公众号