Home Backend Development PHP Tutorial Detailed explanation of common PHP algorithms or functions

Detailed explanation of common PHP algorithms or functions

Mar 30, 2018 pm 01:14 PM
php function Detailed explanation

This article mainly shares with you detailed explanations of common PHP algorithms or functions. It is mainly shared with you in the form of code. I hope it can help you.

<?php

//不用第三个变量,交换两个变量的值
function changeVar()
{
    $a = &#39;123&#39;;
    $b = &#39;456&#39;;
    
    list($a, $b) = array($b, $a);
    
    echo $a . &#39;-&#39; . $b;
}
//文件锁
function fileLock()
{
    $fp = fopen(&#39;./tmp.txt&#39;, &#39;w+&#39;);
    if (flock($fp, LOCK_EX)) { //独占锁定
        fwrite($fp, &#39;write something\n&#39;);
        flock($fp, LOCK_UN);//释放锁
    } else {
        echo "can&#39;t lock!";
    }
    fclose($fp);
}

//获取文件后缀
function getExtName($url)
{
    $path_info = pathinfo($url);
    $ext_name = $path_info[&#39;extension&#39;];
    echo $ext_name;
}

/*斐波那契数列第n位是什么,递归实现
* @param int $n 位数
*/
function fibonacci($n)
{
    $return = 1;
    if ($n <= 0) {
        $return = 0;
    } elseif ($n <= 2) {
        $return = 1;
    } else {
        $return = fibonacci($n - 2) + fibonacci($n - 1);
    }

    return $return;
}

//自己实现number_format
function numFormat($num)
{
    // return number_format($num);
    $num = strrev($num);//反转
    $num = str_split($num, 3);//分割成数组
    $num = implode(&#39;,&#39;, $num);//拼接
    $num = strrev($num);//再反转
    return $num;
}

//冒泡排序
function bubble_sort(&$arr)
{
    $len = count($arr);
    for ($i = 0; $i < $len; $i++) {
        for ($j = 1; $j < $len - $i; $j++) {
            if ($arr[$j - 1] > $arr[$j]) {
                $tmp = $arr[$j];
                $arr[$j] = $arr[$j - 1];
                $arr[$j - 1] = $tmp;
            }
        }
    }
}

//快速排序
function quickSort($arr)
{
    $len = count($arr);
    if ($len <= 1) {
        return $arr;
    }

    $base = $arr[0];
    $left_array = array();
    $right_array = array();
    for ($i = 1; $i < $len; $i++) {
        if ($arr[$i] > $base) {
            $left_array[] = $arr[$i];
        } else {
            $right_array[] = $arr[$i];
        }
    }
    $left_array = quickSort($left_array);
    $right_array = quickSort($right_array);
    return $left_array;
}

//二维数组排序
function array_sort($arr, $keys, $order = &#39;ASC&#39;)
{
    if (!is_array($arr)) {
        return false;
    }
    $keysvalue = array();
    foreach ($arr as $k => $v) {
        $keysvalue[$k] = $v[$keys];
    }

    if ($order == &#39;ASC&#39;) {
        asort($keysvalue);
    } else {
        arsort($keysvalue);
    }
    reset($keysvalue);
    foreach ($keysvalue as $k => $v) {
        $keysort[$k] = $k;
    }
    foreach ($keysort as $k => $v) {
        $new_arr[] = $arr[$v];
    }
    
    return $new_arr;
}
//使用自带函数二维数组排序
function array_sort2(&$arr, $keys, $order = SORT_ASC)
{
    array_multisort(array_column($arr, $keys), $order, $arr);
}

//二分查找
function bin_sch($arr, $low, $top, $target)
{
    sort($arr);
    while ($low <= $top) {
        $mid = floor(($low + $top)/2);
        if ($arr[$mid] == $target) {
            return $arr[$mid];
        } elseif ($arr[$mid] < $target) {
            $low = $mid + 1;
            bin_sch($arr, $low, $top, $target);
        } else {
            $top = $mid - 1;
            bin_sch($arr, $low, $top, $target);
        }
    }
    return -1;
}

//遍历文件夹
function my_scandir($dir)
{
    $files = array();
    if ($handle = opendir($dir)) {
        while (($filename = readdir($handle)) !== false) {
            if ($filename != &#39;.&#39; && $filename != &#39;..&#39;) {
                if (is_dir($dir."/".$filename)) {
                    $files[$filename] = my_scandir($dir."/".$filename);
                } else {
                    $files[] = $filename;
                }
            }
        } 
        closedir($handle);
        return $files;
    }
}

//get_user_id改为getUserId
function change($str)
{
    $str = ucwords($str, &#39;_&#39;);
    return str_replace(&#39;_&#39;, &#39;&#39;, $str);
}

//反转每个单词
function convert($input) {
    $arr = explode(" ", $input);
    array_walk($arr, function (&$value) {
        $value = strrev($value);
    });
    $output =  implode(" ", $arr);
    echo $output;
}
Copy after login

Related recommendations:

2018 Common algorithm questions for front-end interviews

Detailed explanation of common algorithm questions in JavaScript interviews

Several common algorithms for PHP redemption

The above is the detailed content of Detailed explanation of common PHP algorithms or functions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles