user3044500
user3044500

Reputation: 47

Comparing array_sum(string) to an integer in PHP

I came across this line of code while examining the convertFromJSCharcode function in PHPIDS/Monitor.

foreach ($charcode as $char) {
            $char = preg_replace('/\W0/s', '', $char);

            if (preg_match_all('/\d*[+-\/\* ]\d+/', $char, $matches)) {
                $match = preg_split('/(\W?\d+)/', implode('', $matches[0]), null, PREG_SPLIT_DELIM_CAPTURE);

                if (array_sum($match) >= 20 && array_sum($match) <= 127) {
                    $converted .= chr(array_sum($match));
                }

            } elseif (!empty($char) && $char >= 20 && $char <= 127) {
                $converted .= chr($char);
            }
        }

How can I compare array_sum($match) to an integer? My understanding is that array_sum($match) is a string. The same goes for $char.

Upvotes: 1

Views: 472

Answers (3)

Shashank Shah
Shashank Shah

Reputation: 2167

As per my understanding array_sum returns value in

integer or float

if you have an string you may cast it and convert into integer something like (int)$match & then compare.

Upvotes: 1

hjpotter92
hjpotter92

Reputation: 80649

Check the array_sum documentation:

array_sum() returns the sum of values in an array.

[...]

Return Values

Returns the sum of values as an integer or float.

Upvotes: 1

Tuan Duong
Tuan Duong

Reputation: 515

From this document, array_sum will return a number (that is integer or float).

Upvotes: 1

Related Questions