Jump to content

function


Kingy

Recommended Posts

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

Link to comment
Share on other sites

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');

 

 

 

 

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ()

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.