What's the difference between starting an empty room and starting right with the value?

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';
    
asked by anonymous 20.10.2017 / 17:10

5 answers

13

One 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();

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

    
20.10.2017 / 17:42
9

The question how it was presented does not make sense because in both it 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 you did:

$var;
$var = 'teste';

But note that in this example is always a waste because it is declaring or initializing the variable, not using it at all and then changes its value, ie, it was just 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.

See:

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

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

    
20.10.2017 / 18:09
3

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

For example, this would work:

$a = [];

$a[] = 1;

$b[] = 1;

So it would give error:

$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 examples above, we realize that the values of array can be set even without declaring the variable before, but we realize that this can be a big problem if you use a variable with different types accidentally.

I have to myself that it is always important to explain what you are doing.

    
20.10.2017 / 18:22
0

If you happen to start a variable like this:

$x;

It has to be initialized somewhere because it may have memory garbage. 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 how the variable is null, others do not support it.

$x = null;

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

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

$x  = 'teste';

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

24.10.2017 / 16:08
0

If you declare a variable $var; and using it in some part of the code without being assigned value, the memory garbage can cause the program to make an error or worse, it can create an inappropriate behavior that will be difficult to detect.

    
14.11.2017 / 19:36