Home > php教程 > php手册 > 深入理解php内核阅读1

深入理解php内核阅读1

WBOY
Release: 2016-06-13 08:54:24
Original
1117 people have browsed it

深入理解php内核阅读1

do{ }while(0)使用的解释

 

 do{ }while(0)写法的目的主要是为了程序基础函数的健壮性和通用性,和一些代码的灵活使用。

 do{ }while(0)写法会让内部的代码执行一次后退出,如果不使用这种写法,会对那些如if语句后面不紧跟{}的程序员造成编译错误,而对那些使用{}编程习惯的则无影响。

举例:

#define SAFE_DELETE(p) do{ delete p; p = NULL} while(0)  //1.使用 do{ }while(0)写法

#define SAFE_DELETE(p)  { delete p; p = NULL} //2.使用{}写法

#define SAFE_DELETE(p)  delete p; p = NULL  //3.什么都不使用的写法

 

if(NULL != p) SAFE_DELETE(p)     //1.无问题 2.无问题 3.有问题,else前面有2条语句,编译失败

else   ...do sth...

 

if(NULL != p) SAFE_DELETE(p);     //1.无问题 2.有问题,括号后面跟;编译失败 3.有问题,else前面有2条       else   ...do sth...                                 //语句,编译失败

 

 

if(NULL != p) {SAFE_DELETE(p)}     //1.无问题 2.无问题 3.无问题

else   ...do sth...

Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template