Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/289542-re-declare-a-variable/
Share on other sites

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.

  • Like 3

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.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.