PHP code example of ihsandevs / php-binary-search

1. Go to this page and download the library: Download ihsandevs/php-binary-search 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/ */

    

ihsandevs / php-binary-search example snippets




hsanDevs\PhpBinarySearch\BinarySearch;

// Create an instance of BinarySearch
$binarySearch = new BinarySearch();

// Set the data and target
$binarySearch->data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
$binarySearch->target = 60;

// Perform the search with the default comparison function
$binarySearch->search();

// Print the result
$binarySearch->printResult(); // Output: Target 60 found at index 5 with 0 iteration(s) in 0 second(s) with memory usage 0 byte(s).



hsanDevs\PhpBinarySearch\BinarySearch;

$binarySearch = new BinarySearch();

// Create an instance of BinarySearch
$binarySearch = new BinarySearch();

// Set the data and target
$binarySearch->data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
$binarySearch->target = 60;

// Define a custom comparison function
$customCompareFn = function ($data, $target) {
    if ($data == $target) {
        return 0;
    }
    return ($data > $target) ? 1 : -1;
};

// Perform the search with the custom comparison function
$binarySearch->search($customCompareFn);

// Print the result
$binarySearch->printResult(); // Output: Target 60 found at index 5 with 0 iteration(s) in 0 second(s) with memory usage 0 byte(s).



...

// Print the result with log
$binarySearch->printResult()->printLog();



...

// Print the result with debug
$binarySearch->printResult()->printDebug();