SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Functions

  1. #1
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    How do you Define and Call Functions?
    Also, How do you validate fields?

    So if the form name is name
    then you do this:

    if ($name == '') { echo "Error! Name no Entered"; }
    is it?

    Thanks
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this...

    Code:
      function checkField($field) {
        $switch = ($field == "") ? 0 : 1;
      return $switch.
      }
    That will return a boolean value - 0 if the string is blank, 1 if it is not. This allows you to do something like this:

    Code:
    $check = checkField($name);
    
      if ($check) {
        echo("Hi, $name!");
      }
      else {
      echo("Invalid name entered.");
      }
    Hope that helps.

  3. #3
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Thanks...Thats a really cool function.
    Okie, Here's my problem I need to clarify.

    It seems php functions must have variables parsed into the function to work?

    it can't just be
    function name {
    }

    ? just like perl's subroutine?

    Thanks
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  4. #4
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no you do not need to.... this is an example from the manual,
    <?
    function small_numbers() {
    return array (0, 1, 2);
    }
    list ($zero, $one, $two) = small_numbers();
    ?>

  5. #5
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup, no problem at all - I sometimes create a small function called "error" - I simply use empty parentheses (like this: "()") after the name of the function.

    I call that function like this:

    error();

    It simply prints out a generic error message, however, the true power of functions lies in accepting arguments and returning values.

  6. #6
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh. I get it..
    but you must put the ()

    ( and thats what I idnd't put..hence my functions were producing errors..ehehs )

    Thanks for your help guys!
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alternately instead of calling the function for every field you want to check, you can use the $HTTP_POST_VARS array inside your function to check all at once.

    function error_checking() {
    global $HTTP_POST_VARS;
    extract($HTTP_POST_VARS);
    if ($name == "") $error[] = "You must put in a name";
    if ($address == "") $error[] = "You must put in an address";
    if ($city == "") $error[] = "You must put in a city";
    return $error;
    }


    Sample usage

    if ($submit) {
    $error = error_checking();
    if (count($error) > 0) {
    foreach($error as $value) {
    print $value."<br>";
    }
    }
    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW! thanks freddy!
    Thats an excellent piece of code!

    Jolting down....=)
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •