This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class animal { | |
| public $name; | |
| public $foot; | |
| function setName($name) { | |
| $this->name = $name; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // ref: https://www.w3resource.com/php-exercises/searching-and-sorting-algorithm/searching-and-sorting-algorithm-exercise-6.php | |
| // it is running "do-while" until no swap performed. | |
| function bubbleSort($array) { | |
| do { | |
| $swapped = false; | |
| for( $i = 0, $c = count( $array ) - 1; $i < $c; $i++ ) { | |
| if( $array[$i] > $array[$i + 1] ) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $a = 'foo'; | |
| $b = 'bar'; | |
| echo "Original value: $a, $b\n"; | |
| [$a, $b] = [$b, $a]; | |
| echo "Result value: $a, $b\n"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // https://en.wikipedia.org/wiki/Quicksort | |
| function quickSort($arr) { | |
| $length = count($arr); | |
| if ($length < 2) return $arr; | |
| $middleIdx = (int) ($length / 2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $array = array(9, 7, 12, 8, 5, 17, 13, 20, 11, 14); | |
| echo "Original Array : ".implode(', ',$array)."\n"; | |
| sort($array); // sort an array by value in ascending order. Index associations are changed based on new order | |
| // rsort($array); // sort an array by value in descending order. Index associations are changed based on new order | |
| // asort($array); // sort an array by value in ascending order and maintain index associations | |
| // arsort($array); // sort an array by value in descending order and maintain index associations | |
| // ksort($array); // sort an array by key in ascending order and of course maintain index associations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function getPrimeNumbers($total) { | |
| // 1 is impossible to be the prime number. 2 is the first prime number. | |
| // the law is prime number cannot be divided by other prime number. | |
| // so our logic here is to try to divide the number that we want to check | |
| // with prime number that is previously identified. | |
| $result = [2]; | |
| for($i = 2; $i <= $total; $i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function fizzBuzz($total) { | |
| for($i = 1; $i <= $total; $i++) { | |
| if($i % 3 == 0 && $i % 5 == 0) { | |
| $result .= "Fizz Buzz\n"; | |
| } | |
| elseif($i % 3 == 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function isPalindrome($string) { | |
| for($i = 0, $j = strlen($string) - 1; $i < strlen($string) / 2; $i++, $j--) { | |
| if($string[$i] != $string[$j]) { | |
| return 'false'; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function threeSummableToZero($arr) { | |
| for($i = 0; $i < count($arr); $i++) { | |
| for($j = 0; $j < count($arr); $j++) { | |
| for($k = 0; $k < count($arr); $k++) { | |
| if($i == $j || $j == $k || $i == $k) continue; | |
| if($arr[$i] + $arr[$j] + $arr[$k] == 0) return "[".$arr[$i].", ".$arr[$j].", ".$arr[$k]."]"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function factorial($n) { | |
| return ($n == 1 ? $n : $n * factorial($n - 1)); | |
| } | |
| echo factorial(6); |