开发者

Variable Scope Question PHP

开发者 https://www.devze.com 2023-01-12 22:58 出处:网络
In the following 开发者_C百科code the variable does not seem to be getting set. Seems simple enough but for some reason this is vexing me.

In the following 开发者_C百科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;


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消