Skip to content

Instantly share code, notes, and snippets.

View fajarwz's full-sized avatar
👨‍💻
Working from anywhere

Fajar Windhu Zulfikar fajarwz

👨‍💻
Working from anywhere
View GitHub Profile
@fajarwz
fajarwz / animal.php
Created July 5, 2021 13:54
PHP OOP example. Create class, object, get-set attribute
<?php
class animal {
public $name;
public $foot;
function setName($name) {
$this->name = $name;
}
@fajarwz
fajarwz / BubbleSort.php
Created July 3, 2021 02:46
Bubble Sort using PHP
<?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] ) {
@fajarwz
fajarwz / app.php
Created July 3, 2021 02:06
Swap value without temp variable in php
<?php
$a = 'foo';
$b = 'bar';
echo "Original value: $a, $b\n";
[$a, $b] = [$b, $a];
echo "Result value: $a, $b\n";
@fajarwz
fajarwz / QuickSort.php
Last active November 19, 2023 05:44
Quick Sort using php
<?php
// https://en.wikipedia.org/wiki/Quicksort
function quickSort($arr) {
$length = count($arr);
if ($length < 2) return $arr;
$middleIdx = (int) ($length / 2);
@fajarwz
fajarwz / app.php
Last active July 3, 2021 00:23
Sort array in php using sort() 🤷‍♂️
<?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
@fajarwz
fajarwz / app.php
Created June 25, 2021 17:10
Get Prime Numbers using PHP
<?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++) {
@fajarwz
fajarwz / app.php
Created June 25, 2021 15:12
Fizz Buzz using PHP
<?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) {
@fajarwz
fajarwz / app.php
Created June 25, 2021 15:01
Palindrome checker using PHP
<?php
function isPalindrome($string) {
for($i = 0, $j = strlen($string) - 1; $i < strlen($string) / 2; $i++, $j--) {
if($string[$i] != $string[$j]) {
return 'false';
}
@fajarwz
fajarwz / app.php
Created June 25, 2021 14:36
Search for 3 numbers that summable to zero using PHP
<?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]."]";
@fajarwz
fajarwz / app.php
Created June 25, 2021 14:07
Factorial using PHP (recursive)
<?php
function factorial($n) {
return ($n == 1 ? $n : $n * factorial($n - 1));
}
echo factorial(6);