PHP code example of arraypress / comparator

1. Go to this page and download the library: Download arraypress/comparator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arraypress / comparator example snippets


Compare::values(
  $value1,
  $value2,
  string $operator,
  ?string $type = null,
  bool $caseSensitive = true,
  bool $useEpsilon = false,
  $value3 = null,
  ?callable $customFunction = null,
  float $epsilon = 1.0e-10,
  ?callable $errorCallback = null
): ?bool

$result = Compare::values( 'hello', 'Hello', '=', false ); // Case-insensitive string comparison
if ( $result ) {
    // Values are considered equal
}

$result = Compare::values( 5, 10, '<' ) ? 'Pass' : 'Fail';
// Expected output: Pass

$result = Compare::values( 0.1 + 0.2, 0.3, '=', 'float', true, true ) ? 'Pass' : 'Fail';
// Expected output: Pass

$result = Compare::values( 'Hello', 'hello', '=', 'string', true ) ? 'Pass' : 'Fail';
// Expected output: Fail (due to case sensitivity)

$customFunction = function ( $a, $b ) {
    return $a == ( $b * 2 );
};
$result = Compare::values( 10, 5, 'custom', null, true, false, 'all', $customFunction ) ? 'Pass' : 'Fail';
// Expected output: Pass

$json1 = '{"name": "John", "age": 30}';
$json2 = '{"name": "John", "age": 30}';
$result = Compare::values( $json1, $json2, '=' ) ? 'Pass' : 'Fail';
// Expected output: Pass

$result = Compare::values( 'test123', '^test', 'regex' ) ? 'Pass' : 'Fail';
// Expected output: Pass

$array1 = [1, 2, 3];
$array2 = [1, 2, 3];
$result = Compare::values( $array1, $array2, '=' ) ? 'Pass' : 'Fail';
// Expected output: Pass

$result = Compare::values( true, true, '=', 'bool' ) ? 'Pass' : 'Fail';
// Expected output: Pass

$obj1 = (object) [ 'key' => 'value' ];
$obj2 = (object) [ 'key' => 'value' ];
$result = Compare::values( $obj1, $obj2, '=', 'object' ) ? 'Pass' : 'Fail';
// Expected output: Pass

$result = Compare::values( '2023-01-01', '2024-01-01', '<' ) ? 'Pass' : 'Fail';
// Expected output: Pass

$result = Compare::values( 'Hello world', 'world', 'contains' ) ? 'Pass' : 'Fail';
// Expected output: Fail

$result = Compare::values( 'Hello world', 'Hello', 'starts' ) ? 'Pass' : 'Fail';
// Expected output: Pass

$result = Compare::values( 'Hello world', 'world', 'ends' ) ? 'Pass' : 'Fail';
// Expected output: Pass

$result = Compare::values( 5, 10, '!=' ) ? 'Pass' : 'Fail';
// Expected output: Pass

$result = Compare::values( 10, 5, '>' ) ? 'Pass' : 'Fail';
// Expected output: Pass

$result = Compare::between( 15, 10, 20 ) ? 'Pass' : 'Fail';
// Expected output: Pass

$result = Compare::notBetween( 5, 10, 20 ) ? 'Pass' : 'Fail';
// Expected output: Pass

$dateToCheck = '2023-06-15';
$startDate = '2023-06-01';
$endDate = '2023-06-30';
$result = Compare::between( $dateToCheck, $startDate, $endDate, 'date' ) ? 'Pass' : 'Fail';
// Expected output: Pass

$dateToCheck = '2023-07-01';
$startDate = '2023-06-01';
$endDate = '2023-06-30';
$result = Compare::notBetween( $dateToCheck, $startDate, $endDate, 'date' ) ? 'Pass' : 'Fail';
// Expected output: Pass