Forum Moderators: coopster

Message Too Old, No Replies

can i apply strlen to the output of a function?

         

al1911

5:13 pm on Jan 23, 2005 (gmt 0)

10+ Year Member



can i apply strlen to the output of a function?
in other words, will this work...

function something() {
echo "heaps of text"; }

if ( strlen(something()) == 0 ) {
echo "the function didn't say anything"; }
else { something(); }

you see, the output of the code above, if it works, would put "heaps of text" on the screen. but the stuff being echoed by my function changes depending on certain variables and sometimes the echo is empty because, depending on what the function finds, there may be nothing to echo. if there is nothing to echo, i want to display a message ("the function didn't say anything", or something to the sort)

dreamcatcher

5:59 pm on Jan 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



al1911,

No, that will not work. One way you can do it is to pass the text into a function and return its length:

function something($text) {
return strlen($text); }

echo something("Heaps of Text");

This should echo 13.

Then you could use:

if ( something() == 0 ) {
echo "the function didn't say anything"; }
else { something(); }

That help any?

dreamcatcher

8:30 pm on Jan 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The if statement should read:

if ( something("Heaps of Text") == 0 ) {
echo "the function didn't say anything"; }
else { something("Heaps of Text"); }

This will only return your length though, not the actual text. Did you want the display as well?

mcibor

8:52 pm on Jan 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Better to return the text, as well as display it:

function ech()
{
$a = "Heaps of text. ";
$a .= "More heaps of text";
echo $a;
return $a;
}

$count = strlen(ech());

al1911

4:13 am on Jan 24, 2005 (gmt 0)

10+ Year Member



thanks guys,
i was able to use what you said and apply it to my situation
my code works and does what i want now
thanx again :)