br3nn4n Posted April 3, 2009 Share Posted April 3, 2009 I have this function: <?php function shortdesc($src) { $result = explode(' ',$string,26); array_pop($result); $desc = implode(' ',$result) . '...'; return $desc; } ?> Which is called: <?php shortdesc($article); ?> It will NOT return any data! It just returns "NULL". Can someone explain why? The function grabs the first 26 words of a string. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/152407-function-not-returning-data/ Share on other sites More sharing options...
Maq Posted April 3, 2009 Share Posted April 3, 2009 You never use the parameter $src in your function... I think you meant: $result = explode(' ',$src,26); Quote Link to comment https://forums.phpfreaks.com/topic/152407-function-not-returning-data/#findComment-800386 Share on other sites More sharing options...
mrMarcus Posted April 3, 2009 Share Posted April 3, 2009 ^ya. in your function shortdesc(), $src is whatever $article is when the function is being called. Quote Link to comment https://forums.phpfreaks.com/topic/152407-function-not-returning-data/#findComment-800438 Share on other sites More sharing options...
br3nn4n Posted April 3, 2009 Author Share Posted April 3, 2009 Oops, my mistake. In my script $string actually IS $src. I just grabbed the code from Crayon Violent's original post and $string is the variable they used. I have no idea why it's not working since the flow seems to be correct. Any ideas at all? This is really kinda vital to my application...argh haha Quote Link to comment https://forums.phpfreaks.com/topic/152407-function-not-returning-data/#findComment-800491 Share on other sites More sharing options...
premiso Posted April 3, 2009 Share Posted April 3, 2009 <?php $output = shortdesc($article); echo $output; ?> I did not see you actually using the return data. Did you do that? If so please post the full relevant code to avoid basic fixes like this. Quote Link to comment https://forums.phpfreaks.com/topic/152407-function-not-returning-data/#findComment-800494 Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 Also - any need for array_pop? You're not adding the data onto anything! Quote Link to comment https://forums.phpfreaks.com/topic/152407-function-not-returning-data/#findComment-800500 Share on other sites More sharing options...
br3nn4n Posted April 3, 2009 Author Share Posted April 3, 2009 If I echo it nothing comes back, and a var_dump of $desc only gives me a NULL Anything I could be missing? I call the function: <?PHP shortdesc($article); ?> and then stick the $desc variable in a mysql query: <?PHP mysql_query(INSERT INTO ....', '$desc'); ?> I'm sure I'm just making some stupid mistake haha Seriously any ideas are hugely appreciated Quote Link to comment https://forums.phpfreaks.com/topic/152407-function-not-returning-data/#findComment-800545 Share on other sites More sharing options...
Maq Posted April 3, 2009 Share Posted April 3, 2009 Also - any need for array_pop? You're not adding the data onto anything! It's meant to remove the last element, not to add anything. But I don't know the reasoning behind it either... br3nn4n, The second param of explode is the LIMIT of how many elements, NOT characters. Your code works for me, can we see your current version? Quote Link to comment https://forums.phpfreaks.com/topic/152407-function-not-returning-data/#findComment-800552 Share on other sites More sharing options...
premiso Posted April 3, 2009 Share Posted April 3, 2009 You need to remember that VARIABLE SCOPE is a big part of this. <?php $desc = shortdesc($article); ?> Since the function returns $desc and $desc is not a global variable (which it should not be), you have to re-assign it to use it outside the function. I would suggest reading up on Functions Quote Link to comment https://forums.phpfreaks.com/topic/152407-function-not-returning-data/#findComment-800569 Share on other sites More sharing options...
br3nn4n Posted April 3, 2009 Author Share Posted April 3, 2009 Yes! That's the problem, I was thinking it was a global / local issue. I'll try that out when I get to a viable computer and report back. That certainly would explain why it's not returning anything and giving me Null values. so, solution = $desc = shortdesc($article); That's what you're saying right? Also the array_pop was included in Crayon Violent's post of that code. It was from a topic of mine posted previously, and I made it into a function for repeat use. I changed the variable names too as mentioned but that (obviously) would have very little effect on anything assuming I call everything correctly. Thanks so much Quote Link to comment https://forums.phpfreaks.com/topic/152407-function-not-returning-data/#findComment-800582 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.