0

i want to use self define function in my script but i don't know how to use self define function.i tried a little but it doesn't work please help out.if you have idea please share it.

 $fname=$_GET['fname'];
    $lname=$_GET['lname'];
    $submit=$_GET['submit'];
    $error=array();

        function validation($fname,$lname,$submit){
            if(isset($submit))
            {
                if(empty($fname)){
                    return "first name field is empty";
                }

                if(!empty($fname)){
                    if(!is_string($fname))
                    {
                        return "please type alphabet in firstname field";
                    }    
                }

                if(empty($lname)){
                    return "last name field is empty";
                }

                if(!empty($lname)){
                    if(!is_string($lname)){
                        return "please type alphabet in lastname field";
                    }
                }
            }
        }

        validation();

if you have any link regarding use of self define functions please share it.

1 Answer 1

2

You can't just call validation() without any parameters, when you ask for them with the line ($fname,$lname,$submit). Call it like validation($_GET['fname'], $_GET['lname'], $_GET['submit']);.

EDIT: Looking at the updated code, you've got a scoping issue essentially. Don't declare the $fname, $lname, etc vars outside of the function. Just pass them in.

Also, you're checking if (!empty($lname)), but that check is pointless. If it was empty, your function would have returned. You can be 100% sure that it is not empty at that point, so there is no point in checking.

A link for further reading on functions in PHP: http://www.php.net/manual/en/functions.user-defined.php

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

4 Comments

:-what i have to change...1)function validation($fname,$lname,$submit) or function validation();
@user1817669 I don't understand your question. You should keep your function definition as is, but change your function call, i.e. where you do validation().
can i parameter to function validation($fname,$lname,$submit){ .....//// } or function validation($_GET['fname'], $_GET['lname'], $_GET['submit']){ .....//// }
@mesh:-i'm getting a blank page.i want validate my html form with the help of self defining function.if you know please help me out how i implement function in my script.thank for your valuable support

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.