<?php /** * Created by PhpStorm. * User: qishou * Date: 15-8-2 * Time: 上午9:12 */ header("content-type:text/html;charset=utf-8"); $arr = array(3,5,8,4,9,6,1,7,2); echo implode(" ",$arr)."<br/>"; //--------------------------------------- // 常用排序算法 //--------------------------------------- //冒泡排序 function BubbleSort($arr){ $length = count($arr); if($length$i;$j--){ if($arr[$j]"; //快速排序 function QSort($arr){ $length = count($arr); if($length "; //选择排序(不稳定) function SelectSort($arr){ $length = count($arr); if($length"; //插入排序 function InsertSort($arr){ $length = count($arr); if($length =0){ $arr[$j+1] = $arr[$j]; $j--; } $arr[$j+1] = $x; } return $arr; } echo '插入排序:'; echo implode(' ',InsertSort($arr))."<br>"; //--------------------------------------- // 常用查找算法 //--------------------------------------- //二分查找 function binary_search($arr,$low,$high,$key){ while($low$arr[$mid]){ $low = $mid+1; } } return -1; } $key = 6; echo "二分查找{$key}的位置:"; echo binary_search(QSort($arr),0,8,$key); //顺序查找 function SqSearch($arr,$key){ $length = count($arr); for($i=0;$i顺序常规查找{$key}的位置:"; echo SqSearch($arr,$key); //--------------------------------------- // 常用数据结构 //--------------------------------------- //线性表的删除(数组实现) function delete_array_element($arr,$pos){ $length = count($arr); if($pos$length){ return "删除位置出错!"; } for($i=$pos-1;$i除第{$pos}位置上的元素后:"; echo implode(' ',delete_array_element($arr,$pos))."<br>"; /** * Class Node * PHP模拟链表的基本操作 */ class Node{ public $data = ''; public $next = null; } //初始化 function init($linkList){ $linkList->data = 0; //用来记录链表长度 $linkList->next = null; } //头插法创建链表 function createHead(&$linkList,$length){ for($i=0;$idata = $i; $newNode->next = $linkList->next;//因为PHP中对象本身就是引用所以不用再可用“&” $linkList->next = $newNode; $linkList->data++; } } //尾插法创建链表 function createTail(&$linkList,$length){ $r = $linkList; for($i=0;$idata = $i; $newNode->next = $r->next; $r->next = $newNode; $r = $newNode; $linkList->data++; } } //在指定位置插入指定元素 function insert($linkList,$pos,$elem){ if($pos$linkList->data+1){ echo "插入位置错误!"; } $p = $linkList; for($i=1;$inext; } $newNode = new Node(); $newNode->data = $elem; $newNode->next = $p->next; $p->next = $newNode; } //删除指定位置的元素 function delete($linkList,$pos){ if($pos$linkList->data+1){ echo "位置不存在!"; } $p = $linkList; for($i=1;$inext; } $q = $p->next; $p->next = $q->next; unset($q); $linkList->data--; } //输出链表数据 function show($linkList){ $p = $linkList->next; while($p!=null){ echo $p->data." "; $p = $p->next; } echo '<br>'; } $linkList = new Node(); init($linkList);//初始化 createTail($linkList,10);//尾插法创建链表 show($linkList);//打印出链表 insert($linkList,3,'a');//插入 show($linkList); delete($linkList,3);//删除 show($linkList); /** * Class Stack * 用PHP模拟顺序栈的基本操作 */ class Stack{ //用默认值直接初始化栈了,也可用构造方法初始化栈 private $top = -1; private $maxSize = 5; private $stack = array(); //入栈 public function push($elem){ if($this->top >= $this->maxSize-1){ echo "栈已满!<br>"; return; } $this->top++; $this->stack[$this->top] = $elem; } //出栈 public function pop(){ if($this->top == -1){ echo "栈是空的!"; return ; } $elem = $this->stack[$this->top]; unset($this->stack[$this->top]); $this->top--; return $elem; } //打印栈 public function show(){ for($i=$this->top;$i>=0;$i--){ echo $this->stack[$i]." "; } echo "<br>"; } } $stack = new Stack(); $stack->push(3); $stack->push(5); $stack->push(8); $stack->push(7); $stack->push(9); $stack->push(2); $stack->show(); $stack->pop(); $stack->pop(); $stack->pop(); $stack->show();
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above has introduced examples of commonly used algorithms and data structures in PHP, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools
