记录这个PHP代码的最佳方法是什么?

时间:2022-02-22 23:14:21

Can you tell me what does the function do and how would you document the following function using Comments:

你能告诉我这个功能做了什么以及如何使用评论记录以下功能:

function tosql($value, $value_type, $is_delimiters = true, $use_null = true) 
    {
        if (is_array($value) || strlen($value)) {
            switch ($value_type) {
                case NUMBER:
                case FLOAT:
                    return preg_replace(array("/,/", "/[^0-9\.,\-]/"), array(".", ""), $value);
                    break;
                case DATETIME:
                    if (!is_array($value) && is_int($value)) { $value = va_time($value); }
                    if (is_array($value)) { $value = va_date($this->DatetimeMask, $value); } 
                    else { return "NULL"; }
                    break;
                case INTEGER:
                    return intval($value);
                    break;
                case DATE:
                    if (!is_array($value) && is_int($value)) { $value = va_time($value); }
                    if (is_array($value)) { $value = va_date($this->DateMask, $value); }
                    else { return "NULL"; }
                    break;
                case TIME:
                    if (!is_array($value) && is_int($value)) { $value = va_time($value); }
                    if (is_array($value)) { $value = va_date($this->TimeMask, $value); }
                    else { return "NULL"; }
                    break;
                case TIMESTAMP:
                    if (!is_array($value) && is_int($value)) { $value = va_time($value); }
                    if (is_array($value)) { $value = va_date($this->TimestampMask, $value); }
                    else { return "NULL"; }
                    break;
                case NUMBERS_LIST:
                case FLOATS_LIST:
                    $values = (is_array($value)) ? $value : explode(",", $value);
                    for ($v = 0; $v < sizeof($values); $v++) {
                        $value = $values[$v];
                        $value = preg_replace(array("/,/", "/[^0-9\.,\-]/"), array(".", ""), $value);
                        if (!is_numeric($value)) {
                            $value = 0;
                        }
                        $values[$v] = $value;
                    }
                    return implode(",", $values);
                    break;
                case INTEGERS_LIST:
                    $values = (is_array($value)) ? $value : explode(",", $value);
                    for ($v = 0; $v < sizeof($values); $v++) {
                        $values[$v] = intval($values[$v]);
                    }
                    return implode(",", $values);
                    break;
                default:
                    $value = addslashes($value);
                    break;
            }
            if ($is_delimiters) {
                $value = "'" . $value . "'";
            }
        } elseif ($use_null) {
            $value = "NULL";
        } else {
            if ($value_type == INTEGER || $value_type == FLOAT || $value_type == NUMBER 
                || $value_type == NUMBERS_LIST || $value_type == FLOATS_LIST || $value_type == INTEGERS_LIST) {
                $value = 0;
            } elseif ($is_delimiters) {
                $value = "''";
            }
        } 
        return $value;
    }

3 个解决方案

#1


Well giving it a quick once over it appears to be a function to safely convert a given value to a string that can be used in an SQL command. If it can't convert it then it give a suitable safe value, 'NULL', so as not to break the code.

好吧,快速给它一次它似乎是一个安全地将给定值转换为可以在SQL命令中使用的字符串的函数。如果它无法转换它,那么它会给出一个合适的安全值'NULL',以免破坏代码。

usage: tosql( mydate, 'DATE' )

Suggest you cut and paste something like the above above the code in a comment...

建议你在评论中剪切和粘贴上面的代码......

#2


Using PHPDocumentor

/**
 * tosql
 * Converts a string to a valid sql string
 * @param string
 * @param string
 * @return string
*/
function tosql($value, $value_type, $is_delimiters = true, $use_null = true) 
{
}

#3


It takes a value and a constant denoting the type of value and turns it into something acceptable in a SQL statement. It supports arrays as well for some types (DATETIME, DATE, TIME, TIMESTAMP, and LIST types). When $is_delimiters is true, it surrounds the item with single quotes (for inserted values and comparison tokens that require them). If $use_null is specified, it provides a NULL token.

它接受一个值和一个表示值类型的常量,并将其转换为SQL语句中可接受的值。它还支持某些类型的数组(DATETIME,DATE,TIME,TIMESTAMP和LIST类型)。当$ is_delimiters为true时,它用单引号括起项目(对于插入的值和需要它们的比较标记)。如果指定$ use_null,则它提供NULL标记。

How to comment code is a matter of opinion, religion, and how much you care about the next yahoo who has to use your code.

如何评论代码是一个意见,宗教问题,以及你关心下一个雅虎谁必须使用你的代码。

#1


Well giving it a quick once over it appears to be a function to safely convert a given value to a string that can be used in an SQL command. If it can't convert it then it give a suitable safe value, 'NULL', so as not to break the code.

好吧,快速给它一次它似乎是一个安全地将给定值转换为可以在SQL命令中使用的字符串的函数。如果它无法转换它,那么它会给出一个合适的安全值'NULL',以免破坏代码。

usage: tosql( mydate, 'DATE' )

Suggest you cut and paste something like the above above the code in a comment...

建议你在评论中剪切和粘贴上面的代码......

#2


Using PHPDocumentor

/**
 * tosql
 * Converts a string to a valid sql string
 * @param string
 * @param string
 * @return string
*/
function tosql($value, $value_type, $is_delimiters = true, $use_null = true) 
{
}

#3


It takes a value and a constant denoting the type of value and turns it into something acceptable in a SQL statement. It supports arrays as well for some types (DATETIME, DATE, TIME, TIMESTAMP, and LIST types). When $is_delimiters is true, it surrounds the item with single quotes (for inserted values and comparison tokens that require them). If $use_null is specified, it provides a NULL token.

它接受一个值和一个表示值类型的常量,并将其转换为SQL语句中可接受的值。它还支持某些类型的数组(DATETIME,DATE,TIME,TIMESTAMP和LIST类型)。当$ is_delimiters为true时,它用单引号括起项目(对于插入的值和需要它们的比较标记)。如果指定$ use_null,则它提供NULL标记。

How to comment code is a matter of opinion, religion, and how much you care about the next yahoo who has to use your code.

如何评论代码是一个意见,宗教问题,以及你关心下一个雅虎谁必须使用你的代码。