|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #14285 Forcing variable declaration
Submitted: 2001-11-29 11:17 UTC Modified: 2013-10-27 15:33 UTC
Avg. Score:4.8 ± 0.5
Reproduced:61 of 61 (100.0%)
Same Version:27 (44.3%)
Same OS:26 (42.6%)
From: pnh102 at psu dot edu Assigned: krakjoe (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: pnh102 at psu dot edu
New email:
PHP Version: OS:


 [2001-11-29 11:17 UTC] pnh102 at psu dot edu
Is there a way to make PHP force you to declare variables like "option explicit" in VBScript or "use strict" in Perl?  If not, will this feature become available?  While this is not really a bug, it might be a nice feature to have available.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-29 11:20 UTC]
set error_reporting to E_ALL, and you'll see all undefined vars. However, it doesn't force the use of it.

 [2001-11-29 11:39 UTC]
this would only catch read usage of undefined variables,
but not assigning values to them



 $typofree = "hallo";


 $xtypofree = "ooops";


nothing in PHP will warn you that you have a typo
in the second assignment, so creating a new variable

... moved to feature requests ...
 [2002-11-19 08:06 UTC] z95kahe at mtek dot chalmers dot se
I realy think this is good idea. I've been looking for some tool to do this kind of checks and found none.

For small scripts there is no need to have variable declaring but there are for longer multi file, multi developer projects. By making the checks an option we can have both modes available.
 [2003-07-16 15:36 UTC] andrew_nefsky at hotmail dot com
This is definitely an important issue.  Not being able to declare variables creates a debugging nightmare!
 [2003-08-13 14:35 UTC] lsemel at yahoo dot com
This is an important issue.  E_ALL doesn't catch everything.  It makes PHP harder to program compared to other languages where you can declare variables (Perl, Java, etc.)
 [2004-07-22 09:49 UTC] jeffrparsons at optushome dot com dot au
I agree this is a very major issue, and it scares people I know away from using PHP. It would be extremely convenient to have warnings on assignment as well - instead of just upon reading a non-existent variable - and instead of creating a variable when a value is first assigned to it, require a separate declaration using a "var" keyword or such. Obviously not as standard, but as an option...
 [2008-06-13 11:27 UTC] gvdefence-ncr at yahoo dot it
This feature is necessary in PHP.
Try this simple example:

error_reporting(E_ALL | E_NOTICE | E_STRICT); //E_STRICT for PHP5
class Test
   var $mispelled_var;

   function Test($test)
      $this->$mispeled_var = $test;
      ERROR: we think to instantiate $this->mispelled_var, but
      we forgot an 'l' and we are instantiating a new var.
      PHP does not raise any error or warning or notice.

All other languages would raise an error or at least a warning.
I love PHP and I want it to grow, this is a neccesary feature.
It is necessary to add a sort of gobal option that when activated makes PHP sensible to these type of errors.
 [2010-12-17 14:23 UTC]
-Package: Feature/Change Request +Package: Scripting Engine problem -Operating System: Redhat Linux 7.2 +Operating System: * -PHP Version: 4.0.6 +PHP Version: *
 [2012-01-04 01:15 UTC] matt at mattandwell dot net
This would be a great option to add in error_reporting, consider the following 

ini_set("error_reporting", E_ALL|E_STRICT);

class test1
    private $m1 = "initial value in t1";
    public function setVarsTest()
        $this->m1 = "in t1";
    public function showValues()

class test2 extends test1
    public function setVarsTest()
        $this->m1 = "in t2";

$t1 = new test1();

$t2 = new test2();

the output is:

)in t1(
)initial value in t1(

changing $m1 to protected solves the issue, but tracking down issues like this 
after a refactoring session could be easier if PHP had this feature.

"$t2->setVarsTest();" would generate a notice in the above code.
 [2013-10-27 15:33 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: krakjoe
 [2013-10-27 15:33 UTC]
In modern PHP, you must declare static variables before they are assigned or referenced, which gives you a clear answer to the original question: Yes, use static variables.

The comments here confuse the original question, and send everything awry ...

This simply does not make sense:


 $typofree = "hallo";


 $xtypofree = "ooops";


We cannot possibly raise a warning on the second variable assignment, it is perfectly valid code, with no way to tell when the programmer is misspelling or just programming the idea will simply not work, ever.

I think the original question is probably born of a misunderstanding rather than  a genuine desire to change the way PHP works, or did work.

It doesn't make sense for you to have to declare _everything_ in PHP, so it doesn't make sense to have it as an option.

Some conversations are so stupid, they are not worth having :)
 [2016-02-16 12:53 UTC] azzaazza692 at hotmail dot com
I think what was meant by the original question was something along the lines of:
<?php  option=explicit
  declare $typofree;

  $typofree = "hallo";
  $xtypofree = "ooops";

so as $xtypofree has not been 'declared' as a variable, it would throw a 'variable not declared' notice...
 [2017-01-15 17:46 UTC] habibalex at netzero dot com
The problem is when you use both the mistyped and correctly typed variables if they have both been initialized.
function blah($correct)//correct variable name should be declared here
    $correct = 1;

    $incorrect = 2;//this should throw an error because this is an undeclared variable 

    $new = $incorrect*4;//this should throw an error because it's using an undeclared variable.

    return $new;
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu Dec 08 19:03:42 2022 UTC