Results 1 to 9 of 9

Thread: Function Question...

  1. #1
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default Function Question...

    I'm pretty good with php and I use functions a lot... they save so much confusion and mess...
    I've seen function validate($username) and i can't figure out how i can use the variable in the parenthesis... it looks like a useful piece of code... what exactly does it do? and how does it work is what i'm wondering about...

    Thanks

  2. #2
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by pear.php.net
    validate [line 46]
    mixed validate( mixed Structures_Form_ElementInterface$element, object $element)

    Validates a value against the rule.

    If the value is not valid according to the rule, this method should return an error message. It is up to the rule to make sure the message is meaningful.

    * Return: true if the value is ok, an error message otherwise.
    * Access: public



    Parameters
    object $element The form element.
    That's what I found on it. There are other forms of validate() but this is the closest I think.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  3. #3
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    is that your own function, or what blizzard stated? If it yours you call it by
    PHP Code:
    validate("value"
    and you use it just like a variable. for example:
    PHP Code:
    function output($string) {
    echo 
    $string;
    }
    output("Hello world!"); 
    that would echo Hello World!
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  4. #4
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    See.. that's what I don't get. Why make things hard? It is 100 times easier to type "Hello World!" than 4 lines of code to say "Hello World!"

    Or even better, if it MUST be php

    echo 'Hello World!'

    and be done with it...

    I just won't ever get these weird languages.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  5. #5
    Join Date
    Jan 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    See the use that Master_script_maker gave was just a simple usage. you probably wouldn't actually use it to display "Hello World". You use functions to do things that would be repetitive. Like lets say you have to calculate what the total income would be in one year with a monthly payment of x.

    Code:
    <?
    function payment($payment){
    $total = $payment * 12;
    
    echo "$".$total."<BR>";
    }
    
    
    payment("123.64");
    payment("256.89");
    payment("400.00");
    ?>
    Output would be:
    $1483.68
    $3082.68
    $4800

    Also this is a simple equation but this comes in handy when you are doing things like calculating the distance between to zip codes by the ark between there latitude and longitude.

    I hope this helps a little more.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    They're also a method of code organisation and self-documentation. There's a principle in coding that says "never document with comments what can be described in code." E.G. $c = sqrt($a * $a + $b * $b);is a piece of code. Non-mathematicians might not realise what this does, so you could comment it:
    Code:
    // obtain the hypotenuse given the other two sides.
    $c = sqrt($a * $a + $b * $b);
    Or, you could write a function:
    Code:
    function get_hypotenuse($side_a, $side_b) {
      return sqrt($side_a * $side_a + $side_b * $side_b);
    }
    Here there's no need to write a comment because the function explains itself. The function is also completely independent from the rest of the code, so you can move it to another file for organisation purposes and it will still make sense. Plus, there are things that you can do with functions that you simply can't do with flat procedural code. Go and learn a functional programming language (Lisp, Haskell, Erlang, Javascript in some cases) for some examples. The LoC is a pretty useless measure in this case when 1/3 lines contains only a single character :-) In something approaching Lisp style:
    Code:
    function get_hypotenuse($side_a, $side_b)
      { return sqrt($side_a * $side_a + $side_b * $side_b); }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    I get the point, and the "need" I just think it ridiculous to write 500 characters of code to "print" 12 characters I could do another way with 12 characters of code.

    If I already have to write the number (in the pay example above) and then have to write "payment" instead of "12 *" is my point.

    I am a coder, and some of the codes I do to print a meaningless small character is needed, I just think it is a tad excessive at times.

    It's also getting to the point the more "advanced" a language becomes the harder it is to do simple things. php3 had small easy functions that php5 now takes 3 times the coding to do the same thing. Why?
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    php3 had small easy functions that php5 now takes 3 times the coding to do the same thing.
    Example?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Isn't it the other way around?
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

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
  •