如何在PHP中使用可选变量创建函数

时间:2016-02-20 21:10:19

标签: php mysql

我需要帮助在PHP中创建一个接受可选变量的函数。到目前为止,这就是我所做的:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    //Introduction on how the password generator works
    cout << "This program will help you generate a passowrd."
        << "\nPlease enter your password in character or number followed by <ENTER> key."
        << "\n*Please note that character must be in lower case letters or digits.*" << endl
        << "Input your Password: ";

    //char to hold user password
    char password;
    //user input for password
    cin.get(password);
    //while loop for password until it hit a blank
    while (password !='\n')
    {
    //if and else if statement for password
        if (password == 'a' || password == 'b' || password == 'c' || password == '1')
        {
            password = '1';

        }

        else if (password == 'd' || password == 'e' || password == 'f' || password == '2')
        {
            password = '2';
        }

        else if (password == 'g' || password == 'h' || password == 'i' || password == '3')
        {
            password = '3';
        }

        else if (password == 'j' || password == 'k' || password == 'l' || password == '4')
        {
            password = '4';
        }

        else if (password == 'm' || password == 'n' || password == 'l' || password == '5')
        {
            password = '5';
        }

        else if (password == 'p' || password == 'q' || password == 'r' || password == '6')
        {
            password = '6';
        }

        else if (password == 's' || password == 't' || password == 'u' || password == '7')
        {
            password = '7';
        }

        else if (password == 'v' || password == 'w' || password == 'x' || password == '8')
        {
            password = '8';
        }

        else if (password == 'y' || password == 'z' || password == '9')
        {
            password = '9';
        }
        else
        {
            password = '?';
        }

        // cout to print new password
        cout <<password;
        //cin.get to restart loop
        cin.get(password);

    }       

    return 0;
}

如果我在没有提供function customFunc($var, $var1){ global $conn; $query = "SELECT * FROM table WHERE col = '{$var}' AND col1 = '{$var1}';"; $result = mysqli_query($conn, $query); return $result; } 的情况下调用该函数,MySQL就不会返回任何行,即使我们有一些行。

现在我还需要做的是如果未设置$var1则从col1中选择全部。我还想扩展该功能,并包含更多可选变量$var1$var2等,如下所示:

$var3

3 个答案:

答案 0 :(得分:0)

您可以通过声明参数的默认值来使用可选参数,如下所示:

功能abc($ a,$ b,$ c =&#39;&#39;){...}

如果可能,您应该在最后可能的位置传递可选参数。这是为了确保以后更改函数时不会破坏代码。

答案 1 :(得分:0)

您需要为参数指定默认值:

function customFunc($var = null, $var1 = null) {
}

比您检查是否存在这些默认值并相应地修改您的查询。

或者您可以按照注释中的建议使用func_get_args()并将这些动态参数映射到这样的列:

function customFunc() {
    $map = [
        'col1', 'col2', 'col3'
    ];

    $where = [];
    $i = 0;
    foreach (func_get_args() as $arg) {
        if (!isset($map[$i])) {
            break;
        }
        $where[]= $map[$i] . '=\'' . $arg . '\'';
        $i++;
    }

    if (empty($where)) {
        return null;
    }

    $query = 'SELECT * FROM table WHERE ' . implode(' AND ', $where) . ';';
    $result = mysqli_query($conn, $query);
    return $result;
}

答案 2 :(得分:0)

试试这个:

function customFunc($var1=null, $var2=null, $var3=null) {
$query = "SELECT * FROM Table;
if ($var1) {
    $query .= " WHERE col1 ='$var1'";
}
 if ($var2) {
    $query .= " AND col2='$var2'";
}
 if ($var3) {
    $query .= " AND col3='$var3'";
}
return mysql_query($query);

}