Jump to content

Limited values for a function's parameters?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
dprimedx

dprimedx

    Newbie

  • Members
  • PipPip
  • 12 posts
Is there a way to limit the values of the parameters passed into a function? Say I have a function setServiceDay() that can only accept values 'Su','M','T','W','Tr','F','Sa'.

Is there a way to set it up so that values other than those can't be passed? Or am I best off just doing checking within the function with an if statement?

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
It is not possible. Simply use:


function fun($param) {

$vals = array("Su", "M", "T", "W", "Tr", "F", "Sa");

if(!in_array($param, $vals)

    return false;

//Continue Code Here

}


#3
dprimedx

dprimedx

    Newbie

  • Members
  • PipPip
  • 12 posts

John said:

It is not possible. Simply use:


function fun($param) {

$vals = array("Su", "M", "T", "W", "Tr", "F", "Sa");

if(!in_array($param, $vals)

    return false;

//Continue Code Here

}

Thanks. That's what I ended up using after posting earlier.