Creating a more flexible function....
I'm trying to learn PHP, and the best way seems to be actually try to create
my own functions etc. to understand the language rather than staring and
tweaking other peoples codes as I have done. Currently I'm working on
functions.
I made a function for checking phone numbers:
function telephone_check($var)
{
//check if variable exists
if (!$var)
{
echo "Please fill in the field";
}
elseif (!eregi("^[0-9\ \(\)\+\-]{10,}$",$var))
{
echo "That looks like bollox";
}
else
{
echo "More like it!";
}
}
In my pages I can test for the field $telephone with just:
$telephone_check = telephone_check($telephone);
echo $telephone_check;
However, I would like to be able to have some flexibility in the values that
the function echoes back. So that I can put into the function the values I
want for the echoes.
Eg. Have in my function declaration:
function telephone_check($var, $empty, $wrong, $correct)
{
//check if variable exists
if (!$var)
{
echo $empty;
}
elseif (!eregi("^[0-9\ \(\)\+\-]{10,}$",$var))
{
echo $wrong;
}
else
{
echo $correct;
}
}
Where the $emply, $wrong and $correct are the echoed results I want.
I thought I would be able to say in the actual php page:
$telephone_check = telephone_check($telephone, "Pease enter number", "Duff
number", "Looks good");
echo $telephone_check;
But that gives me a error message.
So is there a special syntax for saying "at this position in the function
you're expecting a $variable, but hey, accept the string I've typed as the
value of the variable?
Did that make sense?
|