FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Function not working properly





Star Wars Fanatic
My function (in php) won't work properly. Well, sort of.... Anyways, I have some code that will work outside of a function, but to use it that way, I would have to hard code it a lot, it would be much easier to just call a function and pass it an arguement a bunch of times. But when I put the code into a function, the code doesn't work. Even when I don't pass it an arguement. A function still works, like I can use them to do other things, but when I put the code in there, it won't work. Now the code is this:

Code:
function removeIt($j)   {
   $content[$j] = strtolower($content[$j]);
   $content[$j] = substr_replace($content[$j], '', 0, 18);
   $content[$j] = substr_replace($content[$j], '', -6, 5);
}


I pass it a number, like 12 or something, to use as the key to where it deletes the stuff. I have tried it without passing the key, but that doesn't work. Now it uses an array, now, do I need to pass it the array as an arguement?

Any help would be apprectiated.
[/code]
hexkid
You have three options:

1) pass the array to the function and return a new, changed, array (my preferred option)
Code:
function removeIt(&$array, $j)   {
   $new_array = $array;
   $new_array[$j] = strtolower($new_array[$j]);
   $new_array[$j] = strtolower($new_array[$j]);
   $new_array[$j] = substr_replace($new_array[$j], '', 0, 18);
   $new_array[$j] = substr_replace($new_array[$j], '', -6, 5);
   return $new_array;
}

// ...

$contents = removeIt($contents, 12);


2) pass the array to the function
Code:
function removeIt(&$array, $j)   {
   $array[$j] = strtolower($array[$j]);
   $array[$j] = substr_replace($array[$j], '', 0, 18);
   $array[$j] = substr_replace($array[$j], '', -6, 5);
}

// ...
removeIt($contents, 12);


3) use global variables
Code:
function removeIt($j)   {
   global $contents;
   $content[$j] = strtolower($content[$j]);
   $content[$j] = substr_replace($content[$j], '', 0, 18);
   $content[$j] = substr_replace($content[$j], '', -6, 5);
}

// ...
removeIt(12);
Star Wars Fanatic
Alrite thanks, after I posted this, I realized I could use global variables for this... Rolling Eyes

But thanks a bunch.
Related topics
Strange Mysql-database
SMTP problem
Please help me out... my site has stopped working properly!
[PHP] exit;
move_uploaded_file problem
Not working My Web Site?Help me Plz.....
Autism
Jail Time
Error I can't seem to fix.
r+ on fopen doesn't work
java not working properly
winspooler.exe
A warning with header
AJAX silent bug ?!?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.