How do I force PHP variables to be declared?
Posted: 16 July 2004 12:11 PM   [ Ignore ]  
Research Assistant
RankRankRank
Total Posts:  342
Joined  05-03-2004

I’m embarking on another set of EE extensions, but this time I don’t want to be banging my head against a wall tracking down problems that in any other language would have been compile-time warnings.

Rick mentioned that it is possible to have PHP require that variables be declared before they are used.  Or was it that warnings would be emitted if they weren’t?  I already have error_reporting set to E_ALL.  This will tell me if I’m trying to read an unassigned variable, but it won’t tell me if I’m trying to write to an undeclared one.

Thanks for your help!
~joe

Profile
 
 
Posted: 20 July 2004 06:36 PM   [ Ignore ]   [ # 1 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  9868
Joined  06-19-2002

> it is possible to have PHP require that variables be declared before they are used. Or was it that warnings would be emitted if they weren’t?

If you have debugging turned all the way on (which you do) and you try to use a variable that you haven’t defined then you’ll get a PHP “notice” message.

In order to define the variable, you can just set it at the beginning:

<?php

$test
= "1";

if (
$test == "1")
{
  
echo "yippe!";
}

?>

 Signature 

Chris Curtis
chriscurtis.org

Profile
 
 
Posted: 21 July 2004 05:34 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
RankRankRank
Total Posts:  342
Joined  05-03-2004

Right, that’s attempting to read an unassigned variable.

What if at some point I mean to assign to variable $test but actually type $tset?  PHP will think I’m defining a new variable, when I meant to assign to an existing variable.

I’d rather distinguish between assignment and definition so that PHP knows when I’m making a mistake.  Is there any way to require that all variables be defined with “var” prior to being assigned?

Thanks for your help!
~joe

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64944 Total Logged-in Users: 70
Total Topics: 81925 Total Anonymous Users: 47
Total Replies: 440355 Total Guests: 300
Total Posts: 522280    
Members ( View Memberlist )