Jump to content

Variable without value ....?


eduard

Recommended Posts

Edit: I was thinking Javascript. In PHP? I don't see why somebody would do that. You create a variable with a value when you want to have it in a certain scope but assign a value later from another scope. A basic example is creating global variables:

var a; // This variable now belongs to the global scope and not to a particular function[/s][/s][s][s]function x() {[/s][/s][s][s]  a = 1;[/s][/s][s][s]}[/s][/s][s][s]alert(a) // Undefined[/s][/s][s][s]x();[/s][/s][s][s]alert(a) // Shows "1"
Link to comment
Share on other sites

Why do you want to create a variable without assigning it a value?
it is good practise to intialize variable before it is being used.In PHP like loosy language it does work anyway (with some low level error) but it is still good to intialize it. In strict language variable is declared first mandatoryly so that memory colud be reserve for them. if you have any example code for what that question is arised, posting it will be helpfull
Link to comment
Share on other sites

Consider this example:

try {    my_function_that_may_or_may_not_throw_an_exception();    $var1 = 'value';} catch(Exception $e) {}echo $var1;

If the function throws an exception, $var1 will never be initialized *, so when you reach the "echo", you'll get an error. Explicitly setting the variable before the try block is the solution for this particular example:

$var1;try {    my_function_that_may_or_may_not_throw_an_exception();    $var1 = 'value';} catch(Exception $e) {}echo $var1;

That way, instead of an error, echo will "work", in that it will output NULL (the value NULL converts to an empty string, and thus you'll see nothing). Because of this, and other similar examples, it is a good practice to declare variables, even if you don't have values for them.Of course, if your code doesn't have similar stuff, you don't have to do this, but the point is that doing it anyway is not harmful.* See this page if exceptions are a new thing for you.

Link to comment
Share on other sites

it is good practise to intialize variable before it is being used.In PHP like loosy language it does work anyway (with some low level error) but it is still good to intialize it. In strict language variable is declared first mandatoryly so that memory colud be reserve for them. if you have any example code for what that question is arised, posting it will be helpfull
Thanks!
Link to comment
Share on other sites

Consider this example:
try {	my_function_that_may_or_may_not_throw_an_exception();	$var1 = 'value';} catch(Exception $e) {}echo $var1;

If the function throws an exception, $var1 will never be initialized *, so when you reach the "echo", you'll get an error. Explicitly setting the variable before the try block is the solution for this particular example:

$var1;try {	my_function_that_may_or_may_not_throw_an_exception();	$var1 = 'value';} catch(Exception $e) {}echo $var1;

That way, instead of an error, echo will "work", in that it will output NULL (the value NULL converts to an empty string, and thus you'll see nothing). Because of this, and other similar examples, it is a good practice to declare variables, even if you don't have values for them. Of course, if your code doesn't have similar stuff, you don't have to do this, but the point is that doing it anyway is not harmful. * See this page if exceptions are a new thing for you.

Thanks! But this is still too dificult for me!
Link to comment
Share on other sites

Probably the most common case for declaring a variable without a value is in classes, where you may declare properties that don't have initial values. Example 3 on this page includes a property called $foo that doesn't have an initial value (although it gets a value in the constructor): http://www.php.net/manual/en/language.oop5.visibility.php Otherwise, I always assign variables some default value, even if the value is null.

Link to comment
Share on other sites

Probably the most common case for declaring a variable without a value is in classes, where you may declare properties that don't have initial values. Example 3 on this page includes a property called $foo that doesn't have an initial value (although it gets a value in the constructor): http://www.php.net/m....visibility.php Otherwise, I always assign variables some default value, even if the value is null.
Ok, thanks!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...