Variable declaration in php (is it necessary to declare empty variable?)?

Saiful A

New member
hi.. i've been wondering.. is it really necessary to declare a variable in php? for example, if we declare a variable within a loop and condition, later on, the variable could be out of scope and may not exist.. so, a check is required.. eg:

for($i=0; $i<10; $i++){
if($i==7){
$found="yes";
}
}

if($found!=' '){
echo $found;
}

i found that it is a good practice to declare an array as empty one.. so that we still have a valid empty array even if the test failed.. eg:

$founds=array();

for($i=0; $i<10; $i++){
if($i%2==0){
$founds[ ]=$i;
}
}

print_r($founds);

so.. my question is.. do we have to declare a string or number variable as empty variable? eg:

$found=' ';

for($i=0; $i<10; $i++){
if($i==7){
$found="yes";
}
}

echo $found;
 
Back
Top