Php số 1

Ah, yes, booleans - bit values that are either set [TRUE] or not set [FALSE].  Now that we have 64 bit compilers using an int variable for booleans, there is *one* value which is FALSE [zero] and 2**64-1 values that are TRUE [everything else].  It appears there's a lot more truth in this universe, but false can trump anything that's true...

PHP's handling of strings as booleans is *almost* correct - an empty string is FALSE, and a non-empty string is TRUE - with one exception:  A string containing a single zero is considered FALSE.  Why?  If *any* non-empty strings are going to be considered FALSE, why *only* a single zero?  Why not "FALSE" [preferably case insensitive], or "0.0" [with how many decimal places], or "NO" [again, case insensitive], or .. ?

The *correct* design would have been that *any* non-empty string is TRUE - period, end of story.  Instead, there's another GOTCHA for the less-than-completely-experienced programmer to watch out for, and fixing the language's design error at this late date would undoubtedly break so many things that the correction is completely out of the question.

Speaking of GOTCHAs, consider this code sequence:
$x=TRUE;
$y=FALSE;
$z=$y OR $x;
?>

Is $z TRUE or FALSE?

In this case, $z will be FALSE because the above code is equivalent to rather than as might be expected - because the OR operator has lower precedence than assignment operators.

On the other hand, after this code sequence:
$x=TRUE;
$y=FALSE;
$z=$y || $x;
?>

$z will be TRUE, as expected, because the || operator has higher precedence than assignment:  The code is equivalent to $z=[$y OR $x].

________số 8_______

// Integers
echo 1 1; // 0
echo 1 2; // -1
echo 2 1; // 1

// Floats
echo 1.5 1.5; // 0
echo 1.5 2.5; // -1
echo 2.5 1.5; // 1

// Strings
echo "a" "a"; // 0
echo "a" "b"; // -1
echo "b" "a"; // 1

echo "a" "aa"; // -1
echo "zz" "aa"; // 1

// Arrays
echo [] []; // 0
echo [1, 2, 3] [1, 2, 3]; // 0
echo [1, 2, 3] []; // 1
echo [1, 2, 3] [1, 2, 1]; // 1
echo [1, 2, 3] [1, 2, 4]; // -1

// Objects
$a = [object] ["a" => "b"];
$b = [object] ["a" => "b"];
echo $a $b; // 0

$a = [object] ["a" => "b"];
$b = [object] ["a" => "c"];
echo $a $b; // -1

$a = [object] ["a" => "c"];
$b = [object] ["a" => "b"];
echo $a $b; // 1

// not only values are compared; keys must match
$a = [object] ["a" => "b"];
$b = [object] ["b" => "b"];
echo $a $b; // 1

?>

When using the ++ operator by itself on a variable, ++$var is faster than $var++ and uses slightly less memory [in my experiments].  It would seem like this could be optimized in the language during runtime [if $var++ is the only thing in the whole statement, it could be treated as ++$var].

I conducted many tests [I believe to be fair], and here's one of the results:

$i++ took 8.47515535355 seconds and 2360 bytes
++$i took 7.80081486702 seconds and 2160 bytes

Here's my code.  If anyone sees a bias in it, tell me.  I conducted it many times, each time going through a loop one million iterations and doing each test 10 - 15 times [10 - 15 million uses of the ++ operator].

ini_set[ 'MAX_EXEC_TIME', 120 ];
ob_start[ ];

$num_tests = 10;
$startFirst = $startSecond = $endFirst = $endSecond = $startFirstMemory = $endFirstMemory = $startSecondMemory = $endSecondMemory = $someVal = 0;
$times = array[ '$i++' => array[ 'time' => 0, 'memory' => 0 ], '++$i' => array[ 'total' => 0, 'memory' => 0 ] ];

for[ $j = 0; $j < $num_tests; ++$j ]
{
        for[ $i = 0, $startFirstMemory = memory_get_usage[ ], $startFirst = microtime[ true ]; $i < 10000000; $i++ ]{ $someval = 2; }
        $endFirstMemory = memory_get_usage[ ];
        $endFirst = microtime[ true ];

        for[ $i = 0, $startSecondMemory = memory_get_usage[ ], $startSecond = microtime[ true ]; $i < 10000000; ++$i ]{ $someval = 2; }
        $endSecondMemory = memory_get_usage[ ];
        $endSecond = microtime[ true ];

        $times[ '$i++' ][ $j ] = array[ 'startTime' => $startFirst, 'endTime' => $endFirst, 'startMemory' => $startFirstMemory, 'endMemory' => $endFirstMemory ];
        $times[ '++$i' ][ $j ] = array[ 'startTime' => $startSecond, 'endTime' => $endSecond, 'startMemory' => $startSecondMemory, 'endMemory' => $endSecondMemory ];
}

for[ $i = 0; $i < $num_tests; ++$i ]
{
        $times[ '$i++' ][ 'time' ] += [ $times[ '$i++' ][ $i ][ 'endTime' ] - $times[ '$i++' ][ $i ][ 'startTime' ] ];
        $times[ '++$i' ][ 'time' ] += [ $times[ '++$i' ][ $i ][ 'endTime' ] - $times[ '++$i' ][ $i ][ 'startTime' ] ];
        $times[ '$i++' ][ 'memory' ] += [ $times[ '$i++' ][ $i ][ 'endMemory' ] - $times[ '$i++' ][ $i ][ 'startMemory' ] ];
        $times[ '++$i' ][ 'memory' ] += [ $times[ '++$i' ][ $i ][ 'endMemory' ] - $times[ '++$i' ][ $i ][ 'startMemory' ] ];
}

I conducted many tests [I believe to be fair], and here's one of the results:0

I conducted many tests [I believe to be fair], and here's one of the results:1

I conducted many tests [I believe to be fair], and here's one of the results:2

I conducted many tests [I believe to be fair], and here's one of the results:3

Chủ Đề