This is a discussion on Creating a more flexible function.... within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I'm trying to learn PHP, and the best way seems to be actually try to create my own functions ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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? |
|
|||
|
Try not to use echo directly in functions.
Use return $var; and treat the returned value. -- Have you read the manual ? http://www.php.net/manual/en/ |
|
|||
|
"Clive Sweeting" <eatspam@sweet-apple.co.uk> wrote in message
news:BBFEA18D.5616E%eatspam@sweet-apple.co.uk... > 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? > Ummm not really. If you want to have optional arguments, you can get PHP to 'plug' them with default values such as in: Function myfunc ( $p1=false, $p2=false, $p3=false) { Function innards } So. . . when you call the fiunction you can omit say argument2 as in $thing = myfunc ( 1,, "parameter 3 value") internally you just test for a false value for each parameter to see if it is absent. re-reading your message, you can always call a function with literal strings ("literal string" ) as a parameter just as i have done in the example above, If you want to take the passed strings and send them out as output, yes there is nothing wrong with that. just set up the function so that the appropriate string is returned at the end. You can either have two seperate exit points, each with its own return value, or have a single return point and set the appropriatte retarn value into a varuabe to be returned at the and. - that sounds just as complicated as your question How about another example ; function Myfunc ($test, $good , $bad){ If ($test ) { return $good; }else { return $bad } } // Or function Myfunc ($test, $good , $bad){ $ret = $bad; If ($test ) { $ret = $good; } return $ret; } I prefer the second form HTH Ron |
|
|||
|
On 11/12/03 23:42, in article zd7Cb.476$iJ.413@newsfep1-gui.server.ntli.net,
"Ron" <Ron.Barnett@NTLWorld.com> wrote: > when you call the fiunction you can omit say argument2 > as in > > $thing = myfunc ( 1,, "parameter 3 value") > > internally you just test for a false value for each parameter to see if it > is absent. > > re-reading your message, you can always call a function with literal strings > ("literal string" ) as a parameter just as i have done in the example above, > If you want to take the passed strings and send them out as output, yes > there is nothing wrong with that. > just set up the function so that the appropriate string is returned at the > end. Ok, when I try to put a literal string into my fnction I get: Missing argument 2 for telephone_check() in blah Missing argument 3 for telephone_check() in blah Missing argument 4 for telephone_check() in blah So obviously I'm getting it wrong. It isn't recognising the strings I am puttin I at positions 2,3,and 4 as corresponding to the variable positions in my function as $empty, $wrong, $correct. The code I'm using is... $telephone_check = telephone_check($telephone, "Pease enter number", "Duff number", "Looks good"); The function is: function telephone_check($var, $empty, $wrong, $correct) { //check if variable exists if (!$var) { return $empty; } elseif (!eregi("^[0-9\ \(\)\+\-]{10,}$",$var)) { return $wrong; } else { return $correct; } } So can anyone show me what I am doing wrong here. Is there some special syntax I need to use when declaring the variable that says "I'll be putting a string in this position? |
|
|||
|
On 12/12/03 9:12, in article BBFF36E3.561C6%eatspam@sweet-apple.co.uk,
"Clive Sweeting" <eatspam@sweet-apple.co.uk> wrote: > So can anyone show me what I am doing wrong here. Is there some special > syntax I need to use when declaring the variable that says "I'll be putting > a string in this position? Ah. Looks like if I do this in the function delcaration, it works: function telephone_check($var, $empty = "", $wrong = "", $correct = "") So my function is now: function telephone_check($var, $empty = "", $wrong = "", $correct = "") { //check if variable exists if (!$var) { return $empty; } elseif (!eregi("^[0-9\ \(\)\+\-]{10,}$",$var)) { return $wrong; } else { return $correct; } } Does that look a sensible way of doing things? |
|
|||
|
"Clive Sweeting" <eatspam@sweet-apple.co.uk> wrote in message
news:BBFF3A8D.561CC%eatspam@sweet-apple.co.uk... > On 12/12/03 9:12, in article BBFF36E3.561C6%eatspam@sweet-apple.co.uk, > "Clive Sweeting" <eatspam@sweet-apple.co.uk> wrote: > > > So can anyone show me what I am doing wrong here. Is there some special > > syntax I need to use when declaring the variable that says "I'll be putting > > a string in this position? > > Ah. Looks like if I do this in the function delcaration, it works: > > function telephone_check($var, $empty = "", $wrong = "", $correct = "") > > So my function is now: > > function telephone_check($var, $empty = "", $wrong = "", $correct = "") > { > //check if variable exists > if (!$var) > { > return $empty; > } > elseif (!eregi("^[0-9\ \(\)\+\-]{10,}$",$var)) > { > return $wrong; > } > else > { > return $correct; > } > } > > Does that look a sensible way of doing things? Clive, yes that looks OK now. I'm assuming that in normal circumstances, $empty, $correct & $wrong would be supplied in the call to the function. If you don't intend to supply them ever, cut them out and just return '' where appropriate. Cheers Ron. |
|
|||
|
On 13/12/03 18:18, in article LFICb.195$766.60@newsfep3-gui.server.ntli.net,
"Ron" <Ron.Barnett@NTLWorld.com> wrote: > I'm assuming that in normal circumstances, $empty, $correct & $wrong would > be supplied in the call to the function. Yeah. I was trying to make a set of function for labelling and test form fields. They would have 3 states, and hence 3 labels for the fields in the form Eg. Put email here Incorrect email Emaill looks good > If you don't intend to supply them ever, cut them out and just return '' > where appropriate. I would always put them in. I've adapted the code a bit more so that each function also returns an $error_found variable, so that at the end of the form if (!$error_found==0) { Don't do form } It looks like this. I don't think its the neatest way of doing it, but I ran out of ideas. function mytelephone_check($var, $empty = "", $wrong = "", $correct = "") { // get the $error_check variable in global $errors_found; //check if variable exists if (!$var) { return array( $empty, $errors_found+1, ); } elseif (!eregi("^[0-9\ \(\)\+\-]{10,}$",$var)) { return array( $wrong, $errors_found+1 ); } else { return array( $correct, $errors_found, ); } } Now I'm going to try to do some maths nad date and time stuff (in another post) |