Home Backend Development PHP Tutorial PHP简洁函数小结_php技巧

PHP简洁函数小结_php技巧

May 17, 2016 am 09:16 AM

PHP简洁函数
主题:类菌体PHP简洁函数
简述:PHP简单明了函数语法
适合人群:对开源社区感兴趣,对php感兴趣,有一点时间了解下php
备注:希望大家抛砖,仍蛋,呵呵
1、与mysql相关
mysql_connect
建立一个与MySQL服务器的连接
语法

复制代码 代码如下:

resource mysql_connect(string server[,string usingname[,string password[, bool new_link[,int client_flags]]]])
eg:
$DB_HOST =”localhost”;
$DB_LOGIN =”root”;
$DB_PASSWORD =” 123456”;
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
mysql_data_seek

将内部查询指针移动至查询行上
语法
复制代码 代码如下:

bool mysql_data_seek(resource result_indetifier,int row_number)
eg:
$DB_HOST =”localhost”;
$DB_LOGIN =”root”;
$DB_PASSWORD =”123456”;
$DB_NAME =”flag”;
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
mysql_select_db($DB_NAME);
$res=mysql_query(“SELECT * FROM PRODUCT”);
$row=mysql_fetch_array($res);
for($i=0;$i$row=mysql_fetch_array($res);
mysql_data_seek($res,0);//移动指针回到查询果的第一行
mysql_fetch_array

将查询的结果存在数组中(每一个数组元素存一个记录)
语法
复制代码 代码如下:

array mysql_fetch_array(resource result[,int result_type])
eg
$DB_HOST =”localhost”;
$DB_LOGIN =”root”;
$DB_PASSWORD =”123456”;
$DB_NAME =”flag”;
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
mysql_select_db($DB_NAME);
$res=mysql_query(“SELECT * FROM PRODUCT”);
$row=mysql_fetch_array($res);
mysql_fetch_object
取得查询结果一行,并以对象类型存储之,与MySQL_fetch_array()使用方法完全相同,不同的是mysql_fetch_object()只能通过字段名称来取得查询结果
echo $row->fieldname; //正确用法
echo $row->0; //错误用法

语法
复制代码 代码如下:

object mysql_fetch_object(resource result)
eg
$DB_HOST =”localhost”;
$DB_LOGIN =”root”;
$DB_PASSWORD =”123456”;
$DB_NAME =”flag”;
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
mysql_select_db($DB_NAME);
$res=mysql_query(“SELECT * FROM PRODUCT”);
$row=$mysql_fetch_object($res);
while($row)
{
echo $rowàp_id;
echo $rowàp_name;
}
mysql_insert_id

在使用INSERT命令增加完一条信息后,可利用此函数取得刚刚增加记录的unique id
语法
复制代码 代码如下:

int mysql_insert_id([esource link_identifier])
eg
$DB_HOST =”localhost”;
$DB_LOGIN =”root”;
$DB_PASSWORD =”123456”;
$DB_NAME =”flag”;
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
mysql_select_db($DB_NAME);
$SQLStr”INSERT INTO produce (p_id,p_name)VALUES(‘','PHP书')”;
$res=mysql_query($res);
$p_id=mysql_insert_id();
mysql_num_rows

取得查询结果里有几行
语法
复制代码 代码如下:

int mysql_num_rows(resource result)
eg
$DB_HOST =”localhost”;
$DB_LOGIN =”root”;
$DB_PASSWORD =”123456”;
$DB_NAME =”flag”;
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
mysql_select_db($DB_NAME);
$res=mysql_query(“SELECT * FROM PRODUCT”);
$num=mysql_num_rows($res);
mysql_query

送出一个SQL语法的查询语句
语法
复制代码 代码如下:

resource mysql_query(string query[,resource link_identifier])
eg
$DB_HOST =”localhost”;
$DB_LOGIN =”root”;
$DB_PASSWORD=”123456”;
$DB_NAME =”flag”;
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
mysql_select_db($DB_NAME);
$res=mysql_query(“SELECT * FROM PRODUCT”);
mysql_select_db

选择欲存取的数据库名称
语法
复制代码 代码如下:

bool mysql_select_db(string database_name[,resource link_identifier])
eg
$DB_HOST =”localhost”;
$DB_LOGIN =”root”;
$DB_PASSWORD =”123456”;
$DB_NAME =”flag”;
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
mysql_select_db($DB_NAME);

2、文件系统函数
copy
复制文本
语法
bool copy(string source,string dest)
eg
copy(“abc.txt”,”/tmp/newabc.txt”);
fclose
关闭一个打开文件的指针
语法
bool fclose(resource handle)
eg
$fp=fopen(“abc.txt”,”w”);
fclose($fp);
fgets
从文件指针所指位置取得列的内容
语法
string fgets(resource handle[,int length])
eg
$fp=fopen(“abc.txt”,”w”);
$txtdata= fgets($fp,4096);
file
将整个文件内容读到数组中
语法
array file(string filename[,int use_include_path[,resource context]])
eg
$content=file(“abc.txt”);
file_exists
检查文件是否存在
语法
bool file_exists(string filename)
eg
if (file_exists(“abc.txt”))
echo “此文件存在”;
else
echo”此文件不存在”;
filesize
取得文件大小
语法
int filesize(string filename)
eg
$size=filesize(“abc.txt”);
fopen
打开一个文件或者url
语法
resource fopen (string filename,string mode[,bool use-include_path[,resource zcontext]])
eg
$fp=fopen(“abc.txt”);
$fp=fopen(“http://www.jb51.net/bacteroid/”,”r”);
fputs
将数据写至文件中
语法
int fputs(resource handle,string string[,int length])
eg
$fp=fopen(“abc.txt”);
fputs($fp,”helloworld!”);
fseek
设置文件指针所指的位置
语法
int fseek(resource handle,int offset[,int whence])
eg
$fp=fopen(“abc.txt”,”w”);
$txtdata=fgets($fp,4096);
fseek($fp,0);//将指针指回起始处
mkdir
建立一个目录
语法
bool mkdir(string pathname[,int mode[,bool recursive[,resource context]]])
eg
mkdir(“ljt/newfolder”);
unlink
删除文件
语法
int unlink(string filename);
eg
unlink(”abc.txt”);
3、日期与时间函数
data
返回指定格式的当地时间/日期
语法
string date(string format[,int timestamp])
eg
$time =date(“Y-m-d g:i:s”);
getdate
取得日期与时间的信息
语法
array getdata([int timestamp])
eg
$now=getdate();
$year=$now[“year”];
$month=$now[“month”];
gettimeofday
取得目前的时间(包括格林尼治时间)
语法
array gettimeofday(void)
eg
$time=gettimeofday();
4、字符串处理函数
explode
根据指定的分隔符将字符串拆分成一个数组
语法
array explode(string separator,string string[,int limit])
eg
$str=”a,b,c”;
$res=explode(“,”,$str);//$res[0]=a
implode
将数组内容连接成一个字符串
语法
string implode(string glue,array pieces)
eg
$newarray=array(‘a','b','c');
$res=implode(“,”,$newarray);//$res=a,b,c
strlen
取得字符串的长度
语法
int strlen(string string)
eg
strlen(“www.gxnu.edu.cn”);//传回15
substr
取得字符串指定的某部分字符(子字符串)
语法
string substr(“www.gxnu.edu.cn”,1,7); //返回”ww.gxnu”
5、数学函数库
ceil
将浮点数的小数部分无条件进位
语法
float ceil(float value)
eg
echo ceil(9.99);//返回10
echo ceil(9.12);//返回10
cos
取得浮点数值的余弦值
语法
float cos(float arg)
eg
$numcos=cos(0.5);
floor
将浮点数的小数部分无条件去掉
语法
float floor(floor value)
eg
echo floor(9.12);//返回9
echo floor(9.99);// 返回9
rand
产生一个范围的随机数值
语法
int rand([int min,in max])
eg
$num=rand(0,100);//产生一个介于1到100间的随机数值
round
将浮点数的小数部分四舍五入进位
语法
float round(float value)
eg
float round(9.99)//返回10
float round(9.12)//返回9
sin
取得浮点数值的正弦值
语法
float sin(float arg)
eg
$numsin=sin(0.5);
6、Session函数
session_register
说明一或多个Session里的变量
语法
bool session_register(mixed name[,mixed...])
eg
$name=”flag”;
session_register(“name”);
session_start
初始化Session 信息
语法
bool session(void)
eg
session_start();
7、数组函数
count
计算数组中共有几个数组函数
语法
int count(mixed var[,int mode])
eg
count($array);
list
将数组中的元素值分配给变量
语法
void list(mixed varname,mixed...)
eg
$array=array(a,b,c);
list($str1,$str2,$str3)=$array;//$str1=a
range
建立一个在指定范围内的数组
语法
array range(int low,int high[,int step])
eg
$array=array(0,9);
shuffle
将数组中的元素重新随机排序
语法
bool shuffle(array array)
eg
shuffle($array);
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

Video Face Swap

Video Face Swap

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

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles