1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah i checked my $type variable and it was capitalized... Thanks for giving me the bump!
1

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;
  }
}

1 Comment

Okay, i did declare it, the notice disappears but the variable still does not change inside the if statement. What am i missing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.