VBScript count function

A VBScript equivalent of PHP’s count



Const COUNT_RECURSIVE = 1

Function count(var,mode)

    If not isArray(var) and not isObject(var) Then
        If isNull(var) Then
            count = 0
        Else
            count = 1
        End If
        Exit Function
    End If

    If mode <> COUNT_RECURSIVE Then

        If isArray(var) Then
            count = uBound(var) + 1
        ElseIf isObject(var) Then
            count = var.Count
        End If
        Exit Function

    Else

        Dim key,output : output = 0
        If isArray(var) Then
            For key = 0 to uBound(var)
                If isArray(var(key)) or isObject(var(key)) Then output = output + 1
                output = output + count(var(key),mode)
            Next
        ElseIf isObject(var) Then
            For Each key In var
                If isArray(var(key)) or isObject(var(key)) Then output = output + 1
                output = output + count(var(key),mode)
            Next
        End If

        count = output
    End If

End Function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the array extension