A little help understanding how to use functions
Moderator: General Moderators
A little help understanding how to use functions
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
any help would be great. Thanks
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;
?>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.
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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.
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.
- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
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);
}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.
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
I should probably start my own thread but this seems to apply soo....
Maybe someone could explain the &$var. What's the "&" for.
Maybe someone could explain the &$var. What's the "&" for.
- andre_c
- Forum Contributor
- Posts: 412
- Joined: Sun Feb 29, 2004 6:49 pm
- Location: Salt Lake City, Utah
this should be helpful: ( for the &$var question )
http://php.net/references
http://php.net/references