A little help understanding how to use functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chadbobb
Forum Newbie
Posts: 8
Joined: Mon Nov 08, 2004 8:27 pm

A little help understanding how to use functions

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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;
?>
chadbobb
Forum Newbie
Posts: 8
Joined: Mon Nov 08, 2004 8:27 pm

Post by chadbobb »

but what is something you can do with them. what is there purpose?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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);
}
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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);
}




?>
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

function question

Post 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.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

this should be helpful: ( for the &$var question )
http://php.net/references
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Thanks for the tip. I've looked for that before on php.net and couldn't find it. Thanks again!
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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 ...
Post Reply