Home php教程 php手册 php函数的常用方法及注意之处小结

php函数的常用方法及注意之处小结

Jun 13, 2016 pm 12:07 PM
php code function copy Commonly used method Notice of

复制代码 代码如下:


/**
* @author Yuans
* @copyright php.com
* @package 函数的常用使用方法及特性.
*/
# 基础函数编写注意点.
// 为了方便ide的管理及代码提示功能,我们在所有函数命名时使用fun_开头.
function fun_cutstr($str,$str_width=0,$str_pad='...'){
// 每个函数都得考虑一些异常的情况, 比如函数引入不对, 为0,为false等.
// 由于外部期望返回截取字符后的字符,所以就算此函数不工作,也应该将它传进来的值给返回.
if(empty($str) === true || empty($str_width) === true)
return $str;
// 参数过滤
$str_width += 0;
// 保持一个原则, 尽量不要去污染原始参数,
$return_str = mb_strcut($str,0,$str_width,'utf-8');
// 加强判断, 如果return_str无法有值,由于是mb函数,许多服务器会无法执行.
if(empty($return_str) === false){
return $return_str.$str_pad;
}else{
return $str;
}
}
echo fun_cutstr('aaaaaaaaaaaaaaaaaaaaaaaa',5); // out disply: "aaaaa...";
# 由于是utf-8编码, 所以每个汉字为4字节, 此处将返回"我是...";
echo fun_cutstr('我是个技术工作者',8);
# 或者我们需要考虑对函数的严重破坏,比如如下函数
echo fun_cutstr(false); //out: false
echo fun_cutstr('tbbbbbbbbs','aaaaaaaa'); // out: tbbbbbbbbs
echo fun_cutstr('','aaaaaaaa'); //out: empty
?>


PHP函数的一些基础知识
A: 跟变量命名一样,不可以内置函数名,不可以用数字来命名函数.
B: 重复调用性.
C: 支持静态元素.
D: 支持不固定参数
个人建议技术员对函数做如下规范:
A: 函数名建立分类前缀, 比如字符型的就str_xxx, 布尔型的就 bool_xxxx, 公共函数就 open_xxx 应用型的函数就 APP_xxxx, 临时型的就 temp_xxx
B: 函数的第一步请先判断, 虽然有时自己知道一定会传入某个类型的参数,但作为标准化来说, 先判断再处理是为了程序的健壮也是为了安全.
C: 不要污染原始变量, 如果你有项目经验,有debug应用经验,你就会明白.
D: 引用函数尽量少用,占用内存非常大,损耗严重.
E:不要用大写来编写代码, 不要觉得很cool.
F: 过份产生函数是一种退步的方式, 你可以思考着是否具有重复性, 是否需要包装性, 随意将过程封成函数不是明智之举.
G: 写好你的函数注释.

复制代码 代码如下:


$b = &fun_cutstr('aaaaaaaaaaaaaaaaaaaaaaaa',5); // out disply: "aaaaa...";
fun_cutstr('cccccccccccccccccc',5);
echo $b;
?>


引用函数将在php 5.3版本上无法正常运行, 6.0也最终将其抛弃, 理论上讲echo $b,将会返回ccccc...
$b引入了函数的地址, 为此函数的任何改变都会被赋值给$b.
当然这些真的可以很少用, 不必太在意,特别是新学习者.
静态函数如下表示:

复制代码 代码如下:


/**
* @author Yuans
* @copyright php.com
* @package 函数的常用使用方法及特性.
*/
# 静态函数编写注意点.
function fun_static(){
static $a = 1;
echo $a ++;
}
fun_static();
fun_static();
fun_static();
?>
static $a = 1; 仅会在第一次调用函数时执行, 表明它是个静态, 第二次执行时, $a变量就是取回静态的值, 而不会去执行$a = 1的赋值.如此类推, 数值不停地相加.

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
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)

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 Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

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

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.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles