This topic describes the syntax, description, parameters, return values, and examples of IF functions.

and

This function is described as follows:
  • Syntax: and(arg, ...)
  • Description
    • You can call this function to execute a logical AND operation.
    • Short-circuit semantics is supported. When a value evaluates to false, the subsequent values are not evaluated.
  • Parameters

    arg: the value to check whether is true. Data type: any type. You can specify one or more values.

  • Return values

    This function returns true if all values evaluate to true and returns false if any value evaluates to false.

  • Examples
    if and($arg_mode, eq($arg_mode, 'set_header')) {
        add_rsp_header('USER-DEFINED-1','path1')
    }
    
    Notes
    a. If the request includes the mode parameter and the value of the mode parameter is set_header, set the USER-DEFINED-1 response header.
    b. If the request does not include the mode parameter, the short-circuit semantics takes effect and the subsequent eq comparison is not executed. The response header USER-DEFINED-1 will not be set because the and () function returns false.

or

This function is described as follows:
  • Syntax: or(arg, ...)
  • Description
    • You can call this function to execute a logical OR operation.
    • Short-circuit semantics is supported. When a value evaluates to true, the subsequent values are not evaluated.
  • Parameters

    arg: the value to check whether is true. Data type: any type. You can specify one or more values.

  • Return values

    This function returns true if any value evaluates to true and returns false if all values evaluate to false.

  • Examples
    if and($http_from, or(eq($http_from, 'wap'), eq($http_from, 'comos'))) {
        rewrite(concat('http://tech.com.cn/zt_d/we2015/', $http_from), 'enhance_redirect')
    }
    
    Notes
    a. If the request includes the from header and its value is wap or comos, the URL is 302 rewritten as http://tech.com.cn/zt_d/we2015/[wap|comos].
    b. If the request includes the from header and its value is wap, the short-circuit semantics takes effect and the subsequent eq comos comparison is not executed. At the same time, the or () function returns true.

not

This function is described as follows:
  • Syntax: not(arg)
  • Description

    You can call this function to execute a logical NOT operation. The values of undef and false evaluate to false, and other values evaluate to true.

  • Parameters

    arg: the value to convert to its opposite value. Data type: any type. You can specify only one value.

  • Return values
    • true
    • false
  • Examples
    if not($arg_key) {
        exit(403)
    }
    Note: If a request does not include the key parameter, the request is denied and status code 403 is returned.
    
    if not($cookie_user) {
        exit(403, 'not cookie user')
    }
    Note: If a request does not include cookie_user, the request is denied, a response that contains "not cookie user" is returned with status code 403.
    
    if not(0) {
        exit(403)
    }
    Note: The not (0) function returns false.
    
    if not(false) {
        exit(403)
    }
    Note: The not (false) function returns true.

eq

This function is described as follows:
  • Syntax: eq(arg1, arg2)
  • Description

    You can call this function to compare whether the two values are equal.

  • Parameters
    • arg1: the first value to compare. Data type: any type.
    • arg2: the second value to compare. Data type: same as the arg1 parameter.
  • Return values

    This function returns true if the two values are equal and returns false if they are not equal.

  • Examples
    key1 = 'value1'
    key2 = 'value2'
    if and($arg_k1, $arg_k2, eq(key1, $arg_k1), ne(key2, $arg_k2)) {
        say('match condition')
    }
    
    Notes
    a. If the request includes both the k1 and k2 parameters, the subsequent comparison operations are executed.
    b. If the request does not include the k1 or k2 parameter, the short-circuit semantics take effect and the subsequent comparison operations are not executed.
    c. eq: whether the value of the k1 parameter is equal to value1.
    d. ne: whether the value of the k2 parameter is not equal to value2.
    e. The response that contains "match condition" is returned only if the following conditions are met: the request includes both the k1 and k2 parameters, the value of the k1 parameter is equal to value1, and the value of the k2 parameter is not equal to value2.

ne

This function is described as follows:
  • Syntax: ne(arg1, arg2)
  • Description

    You can all this function to compare whether the two values are not equal.

  • Parameters
    • arg1: the first value to compare. Data type: any type.
    • arg2: the second value to compare. Data type: same as the arg1 parameter.
  • Return values

    This function returns true if the two values are not equal and returns false if they are equal.

  • Examples
    key1 = 'value1'
    key2 = 'value2'
    if and($arg_k1, $arg_k2, eq(key1, $arg_k1), ne(key2, $arg_k2)) {
        say('match condition')
    }
    
    Notes
    a. If the request includes both the k1 and k2 parameters, the subsequent comparison operations are executed.
    b. If the request does not include the k1 or k2 parameter, the short-circuit semantics take effect and the subsequent comparison operations are not executed.
    c. eq: whether the value of the k1 parameter is equal to value1.
    d. ne: whether the value of the k2 parameter is not equal to value2.
    e. The response that contains "match condition" is returned only if the following conditions are met: the request includes both the k1 and k2 parameters, the value of the k1 parameter is equal to value1, and the value of the k2 parameter is not equal to value2.