Stefany93 Posted July 7, 2014 Share Posted July 7, 2014 Howdy colleagues, I have noticed some programmers re-declare their variables before using them. Like so: $name = ''; $name = 'Elizabeth'; First of all, why do they do that? Variables are being re-written with each "=" anyways. Secondly, do you believe this is better than just doing: $name = 'Elizabeth'; Thank you Quote Link to comment https://forums.phpfreaks.com/topic/289542-re-declare-a-variable/ Share on other sites More sharing options...
fastsol Posted July 7, 2014 Share Posted July 7, 2014 Technically no you don't have to declare it and then redeclare it. In your example it would be totally pointless, but there are many times that you should do it or you will get Undefined Variable errors from php. Here's an example of when you woudl do it. $var = ''; foreach(range(1, 10) as $r) { $var .= $r.'<br>'; // If $var wasn't declared before trying to append to it, it woudl throw an error the first time it tried to append to it. } echo $var; It's also helpful to declare vars that will be arrays as empty arrays beforehand just in case the array remains empty and you then try to run it through a foreach loop without checking if it's empty or an array first. 3 Quote Link to comment https://forums.phpfreaks.com/topic/289542-re-declare-a-variable/#findComment-1484176 Share on other sites More sharing options...
Psycho Posted July 7, 2014 Share Posted July 7, 2014 I will second fastsol's response. If the example you posted was exactly what you saw then I would guess it is because someone ran into problems with undefined variables. Then that person received instructions on how to fix it (by defining it beforehand) and mistakenly assumed that it had to always be done. Some other instances where this would be used is any scripts that 'expect' data to be sent from the user, such as form processing scripts. I see a lot of instances of somethign like this if(isset($_POST['submit'])) { //Process the form $name = $_POST['name']; . . . etc. } That would cause a problem if the form was submitted without 'name' in the post data. That not the same as being submitted with an empty value. This shouldn't occur if the user is using your form, but it is bad behavior to assume that all the fields are included in the post data. I typically use something like this $name = isset($_POST['name']) ? $_POST['name'] : ''; It is always a good idea to turn error reporting on to it's highest level to identify undefined variable type errors. They won't typically stop a script or cause an error otherwise. But, doing this will greatly help in preventing lost time in debugging errors. If you were to mistype a variable name and no errors were displayed you might spend a lot of time to find the typo. But, if you enable full error reporting it would be displayed on the page. 1 Quote Link to comment https://forums.phpfreaks.com/topic/289542-re-declare-a-variable/#findComment-1484183 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.