function issue

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
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

function issue

Post 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!
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post 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.
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post 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
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

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



?>
Post Reply