Results 1 to 8 of 8

Thread: Functions and Variables

  1. #1
    Join Date
    Oct 2006
    Posts
    94
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cool Functions and Variables

    I've been reading through about passing variables to functions and I can only find references to using globals but I heard that was a bad idea.

    Here's an example script...
    PHP Code:
    function add_to_database()
    {
    $query "INSERT INTO table (name,age) VALUES ('".$name."', '".$age."')";
    mysql_query($query);
    }
    function 
    send_mail()
    {
    //using the same varibales to send an email
    }
    if (isset(
    $_POST['submit']))
    {
    $name $_POST['name'];
    $age $_POST['age'];
    add_to_database();
    send_mail();
    }
    else
    {
    //the form bit

    The project I'm working on, I'd like to make use of a lot of functions. Most will require the same variables but all variables are based on user input.

    I did read about using the define constant function but my variables are not constant so I don't think it would be any good to me.

    Any ideas?

    Thanks in advance.

    Smithster.

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    The PHP Manual is at http://www.php.net/manual/en/index.php; please refer to it for your next question. I would strongly prefer that you read through it, but some people might not take such a suggestion seriously.

    The specific page you're needing is http://www.php.net/manual/en/functions.arguments.php.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  3. #3
    Join Date
    Oct 2006
    Posts
    94
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Well I'd never read through that part of the php manual before, but I did read up on function scopes yesterday at php.net and it talked about using globals which does get this working. But as I said, I've heard it's a bad idea and so want to learn to do it the right way. The problem is that what I have just read I don't understand! I even read through all the user comments to see if there was something there that might give me a clue.

    I know a lot of members come across a problem and they post it asking for a solution. Yes I am asking for a solution to my problem but I really have researched it first!! I've spent hours pulling my hair out with this one, so tempted to just take the risk and use globals without knowing what the risks are!!! I have read through dynamic drive forums as well as other forums and also gone back to basics at w3schools!! I've also searched in the search engines but can't find anything that explains this in a way that I can understand!

    Please if anyone has an understanding of a safe way to pass variables to a function, show some compassion and help a poor soul out!!!

    Thanks in advance

    Smithster.

  4. #4
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    There's a time to use functions, and a time to not...
    PHP Code:
    function add_to_database($name$age)
    {
    $query "INSERT INTO table (name,age) VALUES ('".$name."', '".$age."')";
    mysql_query($query);
    }
    function 
    send_mail($name$age)
    {
    //using the same varibales to send an email
    }
    if (isset(
    $_POST['submit']))
    {
    $name $_POST['name'];
    $age $_POST['age'];
    add_to_database($name$age);
    send_mail($name$age);
    }
    else
    {
    //the form bit


  5. The Following User Says Thank You to techietim For This Useful Post:

    smithster (08-06-2008)

  6. #5
    Join Date
    Oct 2006
    Posts
    94
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    lol thanks for that Tim. Much appreciated.
    There's a time to use functions, and a time to not...
    The time to not use functions, would that be while I have no clue how to properly use them!!!

    I was advised to learn to use them because I tend to repeat parts in my scripts a few times and I heard that I could create a function and have it called on whenever I needed that part of the script. The above coding is not part of the script I am working on, just a quick example I put together to keep things easy and simple, especially for others who may need help in this area.

  7. #6
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Tim beat me to the Submit button, but here's my reply.

    Code:
    function foo($bar){
        echo 'Hello, ' . $bar . '!';
    }
    foo('world');
    That assigns $bar = 'world'; for the duration of foo, and ultimately prints 'Hello, world!'.

    By the way, global variables aren't absolutely evil. But if you really want to avoid them, you can do something like this:
    Code:
    function main($name, $age){
        addtodatabase($name, $age);
        send_mail($name, $age);
    }
    if($_POST['submit']){
        main($_POST['name'], $_POST['age']);
    }
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

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

    Default

    Programming style 101:
    1. Do not use global variables in your functions. Keep functions pure if possible (that is, written in such a way that calling them twice with the same parameters will always return the same result). Some functions must be impure, but they should be very-top-level wrappers over the pure functions handling only the impure parts of the process.
    2. Generalise generalise generalise! PHP isn't a very nice language for more general programming, but a large function can almost always be broken down into several very general functions and then built up with minor amounts of glue. See, for example, array_map() and friends.
    3. Don't be afraid to create functions. Function-calling overhead is minimal, and you might need it later — especially if it's particularly general.


    See also: A Simple PHP Tutorial.
    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. #8
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Quote Originally Posted by techietim View Post
    There's a time to use functions, and a time to not...
    PHP Code:
    function add_to_database($name$age)
    {
    $query "INSERT INTO table (name,age) VALUES ('".$name."', '".$age."')";
    mysql_query($query);
    }
    function 
    send_mail($name$age)
    {
    //using the same varibales to send an email
    }
    if (isset(
    $_POST['submit']))
    {
    $name $_POST['name'];
    $age $_POST['age'];
    add_to_database($name$age);
    send_mail($name$age);
    }
    else
    {
    //the form bit

    Oooh, I see a MySQL injection attack happening in a matter of time! Please for the love of whoever, escape your strings for safe MySQL usage! This prevents nasty hackers adding DELETE DATABASE whatever; in $_POST['name']; $_POST['age'] or whatever value you're passing on.

    Read more about the escape function here.
    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
  •