Home » Php » variables – Is there something in PHP similar to Option Explicit in VB

variables – Is there something in PHP similar to Option Explicit in VB

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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.

Answer:

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.

Answer:

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.