Detailed explanation of common PHP algorithms or functions
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 = '123'; $b = '456'; list($a, $b) = array($b, $a); echo $a . '-' . $b; } //文件锁 function fileLock() { $fp = fopen('./tmp.txt', 'w+'); if (flock($fp, LOCK_EX)) { //独占锁定 fwrite($fp, 'write something\n'); flock($fp, LOCK_UN);//释放锁 } else { echo "can't lock!"; } fclose($fp); } //获取文件后缀 function getExtName($url) { $path_info = pathinfo($url); $ext_name = $path_info['extension']; 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(',', $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 = 'ASC') { if (!is_array($arr)) { return false; } $keysvalue = array(); foreach ($arr as $k => $v) { $keysvalue[$k] = $v[$keys]; } if ($order == 'ASC') { 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 != '.' && $filename != '..') { 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, '_'); return str_replace('_', '', $str); } //反转每个单词 function convert($input) { $arr = explode(" ", $input); array_walk($arr, function (&$value) { $value = strrev($value); }); $output = implode(" ", $arr); echo $output; }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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

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