-1

I'm very new to PHP, so I'm probably missing something very obvious here, but I just can't figure it out.

function get_halfway_index($position, $size) {
  for($i = 0; $i < ($size / 2); $i++){
    if ($position == $size){
      $position = 0;
    } else {
      $position++;
    }
  }
  echo "returning " . $position . "<br>";
  return $potition;
}
echo "value returned: " . get_halfway_index(0, 500) . "<br>";

I have a function, get_halfway_index, that does some stuff and is supposed to return $potition at the end. I print the value before returning it, and it prints the right value- so no problem there. But when I try calling the function, nothing gets returned.

The output is

returning 250
value returned: 
hhaslam11
  • 191
  • 2
  • 7
  • 24

2 Answers2

1

you have a spelling error potition instead of position

function get_halfway_index($position, $size) {
  for($i = 0; $i < ($size / 2); $i++){
    if ($position == $size){
      $position = 0;
    } else {
      $position++;
    }
  }
  echo "returning " . $position . "<br>";
  return $position;
}
echo "value returned: " . get_halfway_index(0, 500) . "<br>";
imox
  • 1,544
  • 12
  • 12
0

There is a typo

return $potition

should be

return $position 
Charlie
  • 22,886
  • 11
  • 59
  • 90
  • ..well that's embarrassing. I thought I was going crazy there. – hhaslam11 Dec 02 '17 at 13:21
  • @hhaslam11 did php did not show error? – imox Dec 02 '17 at 13:32
  • @imox nope. Not sure why, though. – hhaslam11 Dec 02 '17 at 13:37
  • @hhaslam11 you must have your error reporting suppressed. Remember to start error reporting in such condition. You can also use phpfiddle.org to test php code block such as yours. I saw the error there instantly. :-) – imox Dec 02 '17 at 13:39
  • Yes. You can enable error and warning reporting. Look at this question: https://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings – Charlie Dec 02 '17 at 13:44
  • @imox Ahh, yeah- error reporting was off for some reason. I just assumed it was always on, I didn't actually think you could disable/enable error reporting. Like I said, I'm extremely new to PHP, so this is all pretty foreign to me. Thanks though, that'll definitely help in the future. – hhaslam11 Dec 02 '17 at 13:51