2013-12-21 62 views
0

我想在PHP中返回值爲sql注入,但返回正在停止我的功能。這裏是例子:如何從函數返回多次?

function ex($input) { 
    if (strlen($input) > 5) 
    { 
     return $input; 
    } 
    return ":the end"; 
} 

echo ex("helloa"); 

當我使用功能內側返回它結束它和前(「helloa」)==「helloa」而不是「helloa:末」像我想它。

+0

返回的整個POINT是結束函數。 –

+0

這就是'return'的功能....函數的目的是在返回時終止...如果你不想終止函數,那麼不要使用return –

+0

你想要的行爲是什麼? – Oriol

回答

3

連接字符串的函數

如果你想有多個字符串,居然想串連(或加入)他們。你可以繼續將它們加在一個變量中,然後在函數結尾處返回該變量。

function ex($input) { 
    $return = ""; 
    if (strlen($input) > 5) 
    { 
     $return .= $input; 
    } 
    $return .= ":the end"; 

    return $return; 
} 

echo ex("helloa"); 

使用數組來僞迴歸多個值

如果你真的想返回多個值/串,可以改爲告訴函數返回一個數組。你只能通過函數返回一個輸出。

function ex($input) { 
    // this array acts as a container/stack where you can push 
    // values you actually wanted to return 
    $return = array(); 
    if (strlen($input) > 5) 
    { 
     $return[] = $input; 
    } 
    $return[] = ":the end"; 

    return $return; 
} 

// you can use `implode` to join the strings in this array, now. 
echo implode("", ex("helloa")); 
0

使用

function ex($input) { 
    return (strlen($input) > 5 ? $input : '') . ":the end"; 
}