来自 SWIG 生成的 PHP 扩展的标准输出

发布于 2024-09-10 19:55:17 字数 337 浏览 1 评论 0原文

我有以下 C++ 函数:

void foo() {
    std::cout << "bar" << std::endl;
}

我通过 SWIG 将其移植到 PHP。一切都编译良好并且扩展加载正确。我可以从 PHP 调用 foo(),但如果从命令行运行 PHP 脚本,我只能看到 bar 输出。

$ php script.php
bar

如果我在浏览器中加载脚本,则不会出现任何内容。为什么在这种情况下不显示 bar

I have the following C++ function:

void foo() {
    std::cout << "bar" << std::endl;
}

I'm porting this to PHP via SWIG. Everything compiles fine and the extension loads properly. I'm able to call foo() from PHP, but I only see the bar output if I run the PHP script from the command line.

$ php script.php
bar

If I load the script in the browser, nothing appears. Why does it not show bar in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2024-09-17 19:55:19

您不能直接打印到标准输出。当然,这仅在您使用 CLI SAPI 时才有效。使用 php_printf 或其中任何一个:

//Calls php_output_write
#define PHPWRITE(str, str_len)
//Calls php_output_write_unbuffered
#define PHPWRITE_H(str, str_len)
//Idem:
#define PUTC(c)
#define PUTC_H(c)
#define PUTS(str)
#define PUTS_H(str)
int php_write(void *buf, uint size TSRMLS_DC);
int php_printf(const char *format, ...);
int php_output_write(const char *str, size_t len TSRMLS_DC);
int php_output_write_unbuffered(const char *str, size_t len TSRMLS_DC);
//see the rest of main/output.c

You can't print to stdout directly. This will of course only work if you're using the CLI SAPI. Use php_printf or any of these:

//Calls php_output_write
#define PHPWRITE(str, str_len)
//Calls php_output_write_unbuffered
#define PHPWRITE_H(str, str_len)
//Idem:
#define PUTC(c)
#define PUTC_H(c)
#define PUTS(str)
#define PUTS_H(str)
int php_write(void *buf, uint size TSRMLS_DC);
int php_printf(const char *format, ...);
int php_output_write(const char *str, size_t len TSRMLS_DC);
int php_output_write_unbuffered(const char *str, size_t len TSRMLS_DC);
//see the rest of main/output.c
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文