Azu Posted November 9, 2007 Share Posted November 9, 2007 If my code is running fine without initializing variables, should I just leave it as is, or is there a benefit to initializing them? Also, which way is best to initialize? $a=$b=$c=''; or $a='';$b='';$c=''? Or is there a better way then both of these? Link to comment https://forums.phpfreaks.com/topic/76636-initialize-variables/ Share on other sites More sharing options...
The Little Guy Posted November 9, 2007 Share Posted November 9, 2007 The best way would be to do this: $a='';$b='';$c=''; you don't need to initialize variables in php, but sometimes it is a good thing to do, such as if you were making a loop, then it would always be good to set it to a start point. Link to comment https://forums.phpfreaks.com/topic/76636-initialize-variables/#findComment-388057 Share on other sites More sharing options...
GingerRobot Posted November 9, 2007 Share Posted November 9, 2007 I only do this to avoid undefined variable notices - whilst most people run php with error reporting set to ignore notices, i think it's good practice to make your code more compatible. It also seems cleaner to me. Link to comment https://forums.phpfreaks.com/topic/76636-initialize-variables/#findComment-388063 Share on other sites More sharing options...
pocobueno1388 Posted November 9, 2007 Share Posted November 9, 2007 Initializing your variables first will also speed up your script a little. Link to comment https://forums.phpfreaks.com/topic/76636-initialize-variables/#findComment-388064 Share on other sites More sharing options...
Azu Posted November 10, 2007 Author Share Posted November 10, 2007 Initializing your variables first will also speed up your script a little. Thanks! Is it better to initialize all variables at the very start of the script, or right before they are going to be used? Link to comment https://forums.phpfreaks.com/topic/76636-initialize-variables/#findComment-388332 Share on other sites More sharing options...
kratsg Posted November 10, 2007 Share Posted November 10, 2007 The best programming practice, I personally believe is to define ALL my variables at the top of the script like so: $username = '';//this will hold the username for the current user $site_to_check = '';//this will hold the page the user was just one $ip_address = '';//this holds the true/false statement if the user's ip address was banned or not So I look at the top of my script, see all the variables, and I can almost always recognize what page I'm looking at or what each variable is meant to hold... Cause I often have those memory lapses where I forget everything I coded that night and such and such :-P Link to comment https://forums.phpfreaks.com/topic/76636-initialize-variables/#findComment-388336 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.