PHPackages                             yurunsoft/macro - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yurunsoft/macro

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yurunsoft/macro
===============

支持在 PHP 代码中使用类似 C/C++ 中的宏，进行代码预编译。可以方便兼容不同版本和环境下运行的 PHP 代码。

v1.0.8(3y ago)878.7k11MITPHPPHP &gt;=7.2

Since Dec 25Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Yurunsoft/php-macro)[ Packagist](https://packagist.org/packages/yurunsoft/macro)[ RSS](/packages/yurunsoft-macro/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (3)Versions (10)Used By (1)

php-macro
=========

[](#php-macro)

[![Latest Version](https://camo.githubusercontent.com/76b6ccb086ff81752726c891952c251bd91cca4d0d35dc4ca7373350cb5f4a28/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f797572756e736f66742f6d6163726f2e737667)](https://packagist.org/packages/yurunsoft/macro)[![Test](https://github.com/Yurunsoft/php-macro/actions/workflows/test.yml/badge.svg)](https://github.com/Yurunsoft/php-macro/actions/workflows/test.yml)[![Php Version](https://camo.githubusercontent.com/315f5c30e505b9ccc4178451bbd34c2ba5d281d1fa3267304053ed19ab634125/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e312d627269676874677265656e2e737667)](https://secure.php.net/)

介绍
--

[](#介绍)

支持在 PHP 代码中使用类似 C/C++ 中的宏，进行代码预编译。可以方便兼容不同版本和环境下运行的 PHP 代码。

使用
--

[](#使用)

安装：`composer require yurunsoft/macro`

### 支持的宏

[](#支持的宏)

#### 常量

[](#常量)

**宏：**`#define`、`#const`、`#ifdef`、`#ifndef`

**例子：**

```
#ifndef IN_SWOOLE
    # define IN_SWOOLE extension_loaded('swoole')
#endif
#ifdef IN_SWOOLE
#if IN_SWOOLE
\Co\run(function(){
    echo 'hello world';
});
#endif
#endif
```

> 注意：使用宏定义的常量，仅在生成代码时有效，运行时无效

#### 条件语句

[](#条件语句)

**宏：**`#if`、`#else`、`#elif`、`#endif`

**例子：**

```
`。如果有可以拆开使用拼接的方式

**例子：**

```
