Page 1 of 1

A little help understanding how to use functions

Posted: Tue Dec 14, 2004 6:43 pm
by chadbobb
Can someone expain how to use a function a little for me please. I don't really understand a lot about what i can do with the arguments, probably because i don't understand what an argument is. php.net doesn't do a good job explaining it in the manual.

any help would be great. Thanks

Posted: Tue Dec 14, 2004 8:16 pm
by Benjamin
Well, when you send arguments to a function, they have to be in the same order basically...

Code: Select all

<?php
function AddThem($one, $two)
  {
  $three = $one + $two;
  return $three;
  }

$three = AddThem('1','2');
echo $three;
?>

Posted: Tue Dec 14, 2004 8:56 pm
by chadbobb
but what is something you can do with them. what is there purpose?

Posted: Tue Dec 14, 2004 9:09 pm
by rehfeld
well, theres predefined functions in php, and then theres user defined

now, imagine how hard it would be to write scripts w/out any functions at all?

functions allow you to repeat operations easily.

agtlewis gave a very simple example,
but imagine if it did more than just add 2 numbers together,
lets say his function was 10 lines of code

dont you think it would be easier to make a function, and then just

echo AddThem('55', '112'); // outputs 167

than to write the same 10 lines of code each time you need to use the "AddThem" functionality?

remember, the AddThem example was just blatantly simple.

Posted: Tue Dec 14, 2004 9:26 pm
by John Cartwright
functions are just the tip of the iceberg. When you become comfortable with functions you will soon find yourself writting classes for things that you will always repeat. For example,

I have a class to handle all my database functions (connect, execute, etc),
I have a class which handles file uploading
A class to handle my navigational functionality in my website
A class for user handling, etc

Search these forums for OOP style coding, and there have been several discussions of procedural code vs OOP that you might find interesting.

Posted: Tue Dec 14, 2004 9:28 pm
by dull1554
this is a function i use to generate passwords, i would not want to have to write out all these lines everytime i needed it to execute instead its all in a function and i can just call the function which in turn calls allt those lines of code

Code: Select all

function generatepass($passlength = 8,$usecaps = 0) {//set to 1 for capital letters
$vals = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9');
$vals1 = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S',
'T','U','V','W','X','Y','Z');

$pass = '';


for($i = 0; $i < $passlength; $i++) {
	if($usecaps == 0) {
		shuffle($vals);
	    $pass .= $vals[mt_rand(0,35)];
	}
	if($usecaps == 1) {
		shuffle($vals1);
	    $pass .= $vals1[mt_rand(0,61)];
	}
}
return substr(md5($pass), 0, $passlength);
}

Posted: Tue Dec 14, 2004 9:37 pm
by rehfeld
here, i know this can be hard to understand as a begginer. heres another example. still kinda stupid, but hopefully you can see how being able to do these types of things can be extremely usefull.

the more advanced you become, the more you will have need to define your own functions. for extremely simple scripts, you may not see much reason to use them.

Code: Select all

<?php


function log_error_message($severity, $message) {
    if ($severity == 1) {
        $filename = 'not_urgent.log';
    } elseif ($severity == 2) {
        $filename = 'kinda_urgent.log';
    } elseif ($severity == 3) {
        $filename = 'very_urgent.log';
    }

    $fp = fopen($filename, 'a');
    fwrite($fp, $message);
    fclose($fp);

    if ($severity == 3) { // we better email the admin about this one!
        mail('admin@example.org', 'VERY URGENT ERROR!', $message);
    }

}




// so now lets pretend were in a shopping cart script

if (user wants to buy 100 apples but we only have 3 in stock) {
    $severity = 1;
    $message = 'user wanted more apples than we could supply. better keep more in stock next time';
    log_error_message($severity, $message);
}


if (user has tried 5 different credit cards but they are all declined) {
    $severity = 3;
    $message = 'possible stolen credit cards, call the internet police quick!';
    log_error_message($severity, $message);
}




?>

function question

Posted: Tue Dec 14, 2004 10:37 pm
by neophyte
I should probably start my own thread but this seems to apply soo....

Maybe someone could explain the &$var. What's the "&" for.

Posted: Tue Dec 14, 2004 10:43 pm
by andre_c
this should be helpful: ( for the &$var question )
http://php.net/references

Posted: Tue Dec 14, 2004 11:28 pm
by neophyte
Thanks for the tip. I've looked for that before on php.net and couldn't find it. Thanks again!

Posted: Wed Dec 15, 2004 12:55 am
by andre_c
yeah, things like that are a little hard to find with a search engine.
... i remember looking for the $this keyword once, and the @ sign on php.net ...