Home php Initialization of the variable in PHP

Initialization of the variable in PHP

Author

Date

Category

wrote the code wrote … Then I thought all the time before using the variable always making assigning the value that I need. He read the manual for confidence, spending several tests arose questions:

  1. If we use the uninitialized variable we like NOTICE , although in the manual it is written that use uninitialized variables is a very good practice, where? (maybe it is crookedly written in the manual, it was possible because the contrary)

  2. If we try to make a non -ifiedized variable to make a concatenation, we are like notice , although if we write a variable initially NULL , then notice We do not like words, although again, if we bring an uninitialized variable through VAR_DUMP We will get NULL … It turns out in one case there is Notice , and There is no other, why?

  3. If we assign a variable value NULL Will it be considered that the variable is initialized? And if we just write a variable and at the end we will put a point with a semicolon Will this be the fact that the variable is uninimitable? (I never seen in the manual so that you just wrote a variable without assignment)

    p.s If I remember some other questions on this topic I write off … I do not know Perhaps my questions are connected with what I do not understand the type null ?


Answer 1, Authority 100%

To start – type NULL

null is the only possible value of the type null.

variable is considered to be NULL if:

  • She was assigned a constant NULL.
  • She has not yet been assigned any meaning.
  • it was removed using unset ().

why it works at all

PHP interpreter uses later binding (dynamic typing – variables are associated with the type at the time of the value assignment), therefore it is possible to perform code with variables that have never been declared.

But nevertheless, there is clearly announced null variable and not announced there is a difference!

uninitialized, null, empty and specified variable

$ null = null; // Set NULL
$ empty = ""; // set and empty
$ True = True; // set and not empty
Unset ($ undefined); // not initialized / not specified (unset (), too, that it was not)

This is how it looks in the PHP interpretator:

i.e. We see that if the variable is not asked, then it is generally empty!

ISSET, EMPTY, IS_NULL functions :

then what is the difference and where to see it ???

When attempting to refer to an uninitialized variable PHP will give the value null and generates E_NOTICE Error!

To check whether the variable existed or it was set to NULL – you need to use the array_key_exists function :

  • Array_Key_exists ('name_of_variable', $ globals); for checking in the global area of ​​visibility
  • Array_Key_exists ('name_of_variable', get_defined_vars ()); to check in the current area of ​​visibility

In the current example, it will work like this:

array_key_exists ('null', get_defined_vars ()); // Be True.
Array_Key_exists ('undefined', get_defined_vars ()); // False will be

Now answers to questions

  1. If we use an uninitialized variable we like Notice, although in the manual it is written that using uninitialized variables is a very good practice, where? (maybe it is crookedly written in the manual, it may have been due to the contrary)

In the manual it is written that although initializing and not necessarily, but it is considered good practice!

  1. if we try to make a non-fundamental variable to make a concatenation, we see notice, although if we write a variable initially null, then notice we do not like it, although again if we derive a uninitialized variable via var_dump We will get null … comes out in one There is notice case, but in the other no, why?

About NULL difference and non-specificized wrote above.

  1. If we assign a variable value NULL Will it be considered that the variable is initialized? And if we just write a variable and at the end we will put a point with a comma if it will be the fact that the variable is uninimitable? (In the manual, I never seen so that you just wrote a variable without assignment)

If you assign NULL – yes, it will be initialized.
If you just write and at the end point with a comma – no, it will be uninitialized.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions