Reputation: 904
i've once again something i don't quite understand. I provide you following code:
<?php
class Helper
{
static function SelectDateTimeForm($type)
{
if($type == 'days')
{
$r .= "<select name='something'>";
for ($x = 1; $x <= 31; $x++) {
$r .= "<option value='$x'>$x</option>";
}
$r .= "</select>";
}
return $r;
}
}
?>
So i wanna simply wanna return to whole select stuff inside the $r variable so that i can access it by calling the SelectDateTimeForm() function.
The Problem now is, that while i am inside the if statement (also try'd it with switch case) the $r variable seems to act somehow crazy. When i leave the if and define the $r directly before the return $r everything seems to work.
So why can't i access or modify the $r variable inside the if? And why am i getting the Undefined variable Notice.
Thanks in advice.
Upvotes: 1
Views: 163
Reputation: 2705
declare $r before if to be gloabl variable
class Helper
{
static function SelectDateTimeForm($type)
{
$r = '';
if($type == 'days')
{
$r .= "<select name='something'>";
for ($x = 1; $x <= 31; $x++) {
$r .= "<option value='$x'>$x</option>";
}
$r .= "</select>";
}
return $r;
}
}
Upvotes: 1
Reputation: 11987
Its not because you have multiple assignment, its because you have not declared it. So declare $r
above if()
.
$r = "";
if($type == 'days'){
.....
}
If $type
is not equal to days
, it will not enter if()
, but you are returning $r
which is not declared.
Upvotes: 1