Help me in solving CARVANS problem

My issue

How can I do this in PHP?

My code

<?php

$t = readline();

for($i = 0; $i < $t; $i++) {
    $n = readline();
    $speed = explode(' ', readline());
    
    $j = 1;
    $total = 1; // Because the first car is always moving that the maximum speed.
    while($j < $n) {
        if($speed[$j] < $speed[$j - 1])
            $total++;
    
        $j++;
    }
    print($total . PHP_EOL);
}

Learning course: Level up from 1* to 2*
Problem Link: CodeChef: Practical coding for everyone

Got it. I thought it was a problem with the size of the variable. But It was actually a logical error that was giving me the incorrect result. This is correct:

<?php

$t = readline();

for($i = 0; $i < $t; $i++) {
    $n = readline();
    $speed = explode(' ', readline());
    
    $j = 1;
    $total = 1; // Because the first car is always moving that the maximum speed.
    $slowest = $speed[0];
    while($j < $n) {
        if($n == 1) {
            break;
        }
        else {
            $slowest = min($slowest, $speed[$j]);
            if($speed[$j] <= $slowest) {
                $total++;
            }
        }
        $j++;
    }
    print($total . PHP_EOL);
}