php functions random_color 56


<?php
function random_color_part() {
    return str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT);
}

function random_color() {
    return random_color_part() . random_color_part() . random_color_part();
}
$ar=[];
for ($i=0; $i < 50; $i++) { 
	# code...
	$ar[]= '#'.random_color();
}
$ar[]=json_encode($ar);
print_r($ar);

 

Мегабайты человеко читабельный формат

    private function human_filesize($bytes, $dec = 1)
    {
        $size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
        $factor = floor((strlen($bytes) - 1) / 3);

        return sprintf("%.{$dec}f", $bytes / pow(1024, $factor)) . " " . @$size[$factor];
    }
function isPalindrome($str){
	$strlen = strlen($str);
    for ($i=0; $i<$strlen; $i++){
        if ($str[$i] != $str[$strlen-1-$i] ) {
            return false;
        }
    }
    return true;
}
function isPalindrome($str){
    return $str == strrev($str) ? 
    true : false;
}

 

 

 

 

# php list folder
cat <<EOT> php_folders_index.php
<?php
\$dir = (isset(\$_GET['dir']) ? \$_GET['dir'] : ".");

if (is_dir(\$dir)) {
  echo "<h2>Index of \$dir:</h2>\n";
  \$files = array_diff(scandir(\$dir), array('..', '.'));
  usort(\$files, function(\$a, \$b) {
    if(is_dir(\$a) == is_dir(\$b))
      return strnatcasecmp(\$a, \$b);
    else
      return is_dir(\$a) ? -1 : 1;
  });
  echo "<a href='?dir=" . urlencode(dirname(\$dir)) . "'>..</a><br>\n";
  foreach(\$files AS \$file) {
    echo "<a href='?dir=" . urlencode("{\$dir}/{\$file}") . "'>{\$file}</a><br>\n";
  }
} else if (is_file(\$dir)) {
  header('Content-Description: File Transfer');
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; filename="' . basename(\$dir) . '"');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Content-Length: ' . filesize(\$dir));
  readfile(\$dir);
}
exit;
EOT
php -S localhost:8080 -t . php_folders_index.php

 

Загрузка файла 

cat <<EOT>php_upload_file_index.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File upload and save it</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="foto" onchange="javascript:this.form.submit();">
    <input type="submit">
</form>
<?php
if (\$f = \$_FILES['foto']) {
    \$dest = __DIR__ . '/' . date("U-") . \$f['name'];
    move_uploaded_file(\$f['tmp_name'], \$dest);
    echo "Stored to :<br>\$dest<br>";
    echo "<pre>" . print_r(\$_FILES, true) . "</pre>";
}
?>
</body>
</html>
EOT
php -S 0.0.0.0:4444 -t . php_upload_file_index.php
function fib($num) # n член фибаначи
{
    $fibSum = 0;
    $fib1 = 0;
    $fib2 = 1;

    for ($i = 0; $i < $num; $i++) {
        $fib1 = $fib2;
        $fib2 = $fibSum;
        $fibSum = $fib2 + $fib1;
    }

    return $fibSum;
}

function addDigits($num) { 
    $str = strval($num);
    $res = 0;
    do {
    	$sum = 0;
    	for ($i=0;$i<strlen($str); $i++) { 
    		$sum += intval($str[$i]);
    	}
    	$str = strval($sum);
    	$res = $sum;
    } while(strlen($str)!=1);
    return $res;
}



 

 

function isBalanced($str)
{
    $count = 0;
    for ($i = 0; $i < strlen($str); $i++) {
        $count = $str[$i] === '(' ? $count + 1 : $count - 1;
        if ($count < 0) {
            return false;
        }
    }

    return $count === 0;
}
function isBalanced($str) { 
	if (strlen($str)==0)  return true;
	if (strlen($str)%2 != 0 || substr_count($str,'(') != substr_count($str,')'))  return false;
	while (strlen($str)!=0) {
		if (strpos($str,'()')===false) return false;
		$str = str_replace('()', '', $str);
	}
	return true;
}

 

function fizzBuzz($begin, $end)
{
    for ($i = $begin; $i <= $end; $i++) {
        $hasFizz = $i % 3 === 0;
        $hasBuzz = $i % 5 === 0;
        $fizzPart = $hasFizz ? 'Fizz' : '';
        $buzzPart = $hasBuzz ? 'Buzz' : '';
        print_r($hasFizz || $hasBuzz ? "{$fizzPart}{$buzzPart}" : $i);
        print_r(" ");
    }
}


function fizzBuzz($s, $e){
    $r = [];
    for($i=$s;$i<=$e;$i++){
        if ($i%3==0 && $i%5==0) {
            $r[]='FizzBuzz';
        }
        else {
            if ($i%3==0 || $i%5==0) {
        if ($i%3==0) $r[]='Fizz';
        if ($i%5==0) $r[]='Buzz'; 
            } else $r[]= $i;
        }
        
    }
    echo implode(' ', $r);
}

 

 

<?php
function getChineseZodiac($year)
{
    switch ($year % 12) :
        case 0:
            return 'Monkey';  // Years 0, 12, 1200, 2004...
        case 1:
            return 'Rooster';
        case 2:
            return 'Dog';
        case 3:
            return 'Boar';
        case 4:
            return 'Rat';
        case 5:
            return 'Ox';
        case 6:
            return 'Tiger';
        case 7:
            return 'Rabit';
        case 8:
            return 'Dragon';
        case 9:
            return 'Snake';
        case 10:
            return 'Horse';
        case 11:
            return 'Lamb';
    endswitch;
}

echo getChineseZodiac(2016);