What is the difference between starting an empty variable and starting directly with the value?

Asked

Viewed 1,023 times

13

I see things like:

$arr = [];

$var = '';

$var;

$var = null;

What’s the difference between starting the variable like this:

$var = '';

$var = 'teste';

And start like this:

$var= 'teste';
  • I believe that the only difference is too much line to process, as PHP is not a typed language it makes no difference to assign a value to the variable before or after. You can use both modes according to what you want to do, but the most common is to create a variable and already assign a value to it

  • 1
  • 1

    Each programming language will have a specific behavior for initialized variables with value or not. Mainly in typed and untyped languages.

5 answers

13


An acceptable reason is to avoid undefined, for example...

class teste {
  public $teste = '';

  public function mudar(){
    if(1 < 0){
      $this->teste = 'mudou';
    }
  }

  public function exibir(){
    echo $this->teste;
  }

}

$res = new teste();
$res->mudar();
$res->exibir();

Note that in mudar(), if not true, then teste was not defined, if I had not defined at the beginning of the class, in exibir() we would have an indefinite property

9

The question in the way it was presented does not make sense because in both is starting with value.

The first example starts with a string whose value is no character and then changes to a string with a text.

$var = '';
$var = 'teste';

The second example already starts with the text.

$var = 'teste';

It would be a worthless variable if it did:

$var;
$var = 'teste';

But note that in these examples it is always a waste because it is declaring or initiating the variable, not using it for nothing and then changing its value, that is, it was only having made the last line. It would make sense if you had operations with the variable in a state and then change the state.

If you are not going to use the variable do not create it. Leave it to do this when you need it.

Declaring a variable without value is the same as not declaring.

Behold:

echo gettype($var) . ' - |' . $var . '| - ';
$var;
echo gettype($var) . ' - |' . $var . '| - ';
$var = '';
echo gettype($var) . ' - |' . $var . '| - ';
$var = 'teste';
echo gettype($var) . ' - |' . $var . '| - ';

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

3

If you set the variable with the value of array, it is important to define the definition, even though you can define directly as array, if you define as another type of value can generate errors.

For example, this way it would work:

$a = [];

$a[] = 1;

$b[] = 1;

So it would be wrong:

$c = ' ';

$c[] = 1; // PHP Fatal error:  [] operator not supported for strings

$d = new stdClass;

$d[] = 2; // Uncaught Error: Cannot use object of type stdClass as array

Looking at the above examples, we realize that the values of array can be set even not declaring the variable previously, but we realize that this can be a big problem if use a variable with different types accidentally.

I have for myself that it is always important to explain what is being done.

0

If you happen to start a variable this way:

$x;

It has to be initialized somewhere because it may have memory junk. For example:

$i = $x + 1;

You do not know what can come out of it because it had no initial value. Some programming languages do not allow this, so beware.

If so:

$x = '';

Some languages will understand as the variable being null, others not even support.

$x = null;

This case is self-explanatory, you do not assign any real, but say it exists and already initialized. Beware because it is different from 0.

And if you declare with some previous value, it is an initial value. It can be changed later if the language allows.

$x  = 'teste';

Beware in this case when assigning another string that uses " " instead of ' ', in php they are different things and can generate some error.

0

If you declare a variével $var; and use it in some part of the code without being assigned value, the memory junk can cause the program to crash or worse, can create inappropriate behavior that will be difficult to detect.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.