Joseph U.
Joseph U.

Reputation: 4607

Variable Scope Question PHP

In the following code the variable does not seem to be getting set. Seems simple enough but for some reason this is vexing me.

function teasertext($string){
    $tstring = "";
    if (strlen($string)>9){
        $tstring .= substr($string,0,9) . "....";
    }
    else
    {
        $tstring .= $string;
    }
}
print $tstring;
return $tstring;

Upvotes: 1

Views: 94

Answers (2)

Buggabill
Buggabill

Reputation: 13921

print $tstring;
return $tstring;

is outside of the function block.

function teasertext($string){
    $tstring = "";
    if (strlen($string)>9){
        $tstring .= substr($string,0,9) . "....";
    }
    else
    {
        $tstring .= $string;
    }
    print $tstring;
    return $tstring;
}

Should return $tstring properly.

Upvotes: 2

Joseph U.
Joseph U.

Reputation: 4607

I placed the variables outside of the function. Silly mistake.

Upvotes: -1

Related Questions