Page 1 of 1

function issue

Posted: Sun Jul 27, 2003 9:38 am
by oculasm
I received some help from pootergiest on this forum for stripping line breaks from my arrays.. problem is , Im having trouble creating a function out of it.

Below is the original few lines of code. which work

Code: Select all

//this bit assigns the array element to a var.
$entry_0=($entries[0][entrymsg]);

//This bit strips off extra line breaks.
$entry_0 = str_replace(' 
','', trim($entry_0)); 
//hardcoded line break neccessary.

below is my attempt at making a function out of it. it doesnt work!


Code: Select all

function striplb($tempentry)
{
$tempentry = str_replace(' 
','', trim($tempentry)); 
return $tempentry;
}

$entry_0=($entries[0][entrymsg]);
striplb($entry_0);
print($entry_0);
gives me the result which is entry_0 with the line break! :( Im definately doing something wrong..and I also apologise for being such a newbie amongst the ranks of seasoned developers.. Ive tried most of the stuff Ive found on tutorials/forums before posting here.
Any help on fixing this function would be greatly appreciated..

cheers!

Posted: Sun Jul 27, 2003 11:18 am
by trollll
Because the function returns a value, you need to just assign the function's returned value to the variable. So just something more like

Code: Select all

$entry_0 = striplb($entries[0][entrymsg]);

print($entry_0);
should do the trick. The way that you had it before, the function worked fine, but you just didn't assign the value getting sent back via the return statement to the variable.

Posted: Sun Jul 27, 2003 11:22 am
by trollll
Because the function returns a value, you need to just assign the function's returned value to the variable. So just something more like

Code: Select all

$entry_0 = striplb($entries[0][entrymsg]);

print($entry_0);
should do the trick. The way that you had it before, the function worked fine, but you just didn't assign the value getting sent back via the return statement to the variable.

For future reference, you should probably check out php.net's manual, specifically the section on functions

Posted: Sun Jul 27, 2003 1:25 pm
by oculasm
hey thanks trollll, once again you save my day.
your solution worked a treat.

and I see where I was going wrong now.

I tried all kinds of wierd for loops before settling for that function..
and then I ended up with a headache when I couldnt figure why it wasnt working.

I still have a lot to learn.

cheers.


full function listing: which strips out line breaks from array entry and puts it in a variable called entry_0;

Code: Select all

<?php

function striplb($tempentry) 
{ 
$tempentry = str_replace(' 
','', trim($tempentry)); 
return $tempentry; 
} 


$entry_0 = striplb($entries[0][entrymsg]);



?>