Kingy Posted December 27, 2007 Share Posted December 27, 2007 i'm really very new to creating php functions and i'm attempting one at the moment... function getInfo($name) { $info = explode('!',$name, 2); $user = str_replace(":", '', $info[0]); $user = trim($user); $address = trim($info[1]); } and then in my php code i put... getInfo($ex[0]); $ex[0] returns as :Kingy!k1hosting@csops.bitsjoint.net so what i want the function to do is explode $ex[0] into two.. so it will end up as :Kingy and k1hosting@csops.bitsjoint.net then i want it to replace : with nothing so i can get just straight Kingy and then just to trim both the user and address. and then i want to be able to just echo both $user and $address Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/ Share on other sites More sharing options...
vbnullchar Posted December 27, 2007 Share Posted December 27, 2007 hope this helps <?php function getInfo($name) { list($user, $address) = explode("!", $name); echo $user; echo $address; } $ex[0] = "Kingy!k1hosting@csops.bitsjoint.net"; getInfo($ex[0]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/#findComment-423865 Share on other sites More sharing options...
Kingy Posted December 27, 2007 Author Share Posted December 27, 2007 thanks for the reply... if i have <?php function testfunc($test) { echo "this is $test"; } while (...) { data here testfunc(Kingy); } ?> my question is - will this function work if it is out of the loop? Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/#findComment-423912 Share on other sites More sharing options...
PFMaBiSmAd Posted December 27, 2007 Share Posted December 27, 2007 When learning programming, trying something is a great way to learn. This part is the function definition - function testfunc($test) { echo "this is $test"; } You can call it any way you want in the program. Note: You would need to use quotes around a parameter to make it a string - testfunc('Kingy'); Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/#findComment-423919 Share on other sites More sharing options...
Kingy Posted December 27, 2007 Author Share Posted December 27, 2007 thats weird because with my little test code i have... <?php //i have more code than this, but this is the important stuff. function test($name) { echo "$name"; fputs($conn,"PRIVMSG #Lobby User: $name \r\n"); } while(1) { if($cmd == ":`test") { test('Kingy'); } } ?> That is the code that i am trying to work with, once i can get this function working ill move on to the rest... The code below will work <?php //the rest of everything up here minus the function while(1) { if($cmd == ":`test") { $name = 'Kingy'; fputs($conn,"PRIVMSG #Lobby User: $name \r\n"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/#findComment-423981 Share on other sites More sharing options...
Kingy Posted December 27, 2007 Author Share Posted December 27, 2007 sorry i havn't posted the error i get Kingy Warning: fputs(): supplied argument is not a valid stream resource in C:\xampp\htdocs\bot.php on line 18 as u can see.. it echos 'Kingy' like it should do, but it doesn't seem to want to fput it in Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/#findComment-423986 Share on other sites More sharing options...
PHP_PhREEEk Posted December 27, 2007 Share Posted December 27, 2007 if $conn is defined outside of the function, it must be imported in.. otherwise the function sees $conn as a non-existent variable (this is referred to as 'scope'). <?php function some_Function($name) { global $conn; // rest of function } PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/#findComment-423988 Share on other sites More sharing options...
Kingy Posted December 27, 2007 Author Share Posted December 27, 2007 thats exactly what i want!!! Thank you PhREEEk My next question is.. can i do something like this function test(...) { $somevar = something; } then somewhere else in the code just type echo "$somevar"; ?? or do i have to echo it in the function () Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/#findComment-423993 Share on other sites More sharing options...
PHP_PhREEEk Posted December 27, 2007 Share Posted December 27, 2007 Variables created inside the function are lost when the function is exited. You can, however, RETURN the value and assign it to a variable, like so: <?php function someFunction($name) { global $conn; // some stuff // more stuff $someVar = 'some stuff'; return $someVar; } echo "someVar = $someVar<br>"; // prints someVar = nothing... because someVar doesn't exist $someVar = someFunction('a_name'); echo "someVar = $someVar<br>"; // prints someVar = some stuff PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/#findComment-423999 Share on other sites More sharing options...
Kingy Posted December 27, 2007 Author Share Posted December 27, 2007 so if i did this.. <?php function splitinfo($name) { $info = explode("!", '', $name); $user = $info[0]; $address = $info[1] RETURN $user; RETURN $address; } splitinfo($ex[0]); //ex[0] looks like Kingy!k1hosting@address.com so really $user = Kingy and $address = k1hosting@address.com echo "$user"; echo "$address"; ?> so would that work? or no Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/#findComment-424015 Share on other sites More sharing options...
PHP_PhREEEk Posted December 27, 2007 Share Posted December 27, 2007 Nope, you can only return one value, and you have to assign it to a variable. If you have more than one value to return, you can return it as an array, then access the elements or loop over it, etc... <?php function splitinfo($name) { $info = explode("!", '', $name); $user = $info[0]; $address = $info[1] RETURN array($user, $address); } $splitInfo = splitinfo($ex[0]); //ex[0] looks like Kingy!k1hosting@address.com so really $user = Kingy and $address = k1hosting@address.com echo $splitInfo[0]; echo $splitInfo[1]; ?> PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/#findComment-424021 Share on other sites More sharing options...
Kingy Posted December 27, 2007 Author Share Posted December 27, 2007 ok thanks for all your help. Topic solved. if i have any more issues later i'll create a new topic, but i should be all good for now. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/#findComment-424028 Share on other sites More sharing options...
PHP_PhREEEk Posted December 27, 2007 Share Posted December 27, 2007 ok thanks for all your help. Topic solved. if i have any more issues later i'll create a new topic, but i should be all good for now. Thanks! All right then, I think you've learned a few things about using a function. Just a note to not go crazy with them. There are fairly specific reasons for using them, and they are not designed to replace normal body of code. Basically, if you find yourself typing the same block of code over and over again, it's probably time to put that code in a function. Also, functions work great for doing very specific or specialized things. Sometimes they return values, other times they don't. They are only a tool, not the entire tool box! Good luck man! PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/#findComment-424033 Share on other sites More sharing options...
Kingy Posted December 27, 2007 Author Share Posted December 27, 2007 well it sounds like i needa use it then because for every command i need to explode $ex[0] into user and address, trim both of them down, connect to mysql and check whether or not the user and address is in the database, and only then can they operate the command thanks again Quote Link to comment https://forums.phpfreaks.com/topic/83309-function/#findComment-424046 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.