Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/152407-function-not-returning-data/
Share on other sites

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

If I echo it nothing comes back, and a var_dump of $desc only gives me a NULL :P

 

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

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?

 

 

 

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

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

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.