PHP和C ++可以在对方之间传递数据吗? [英] Can PHP and C++ pass data between each other?

查看:147
本文介绍了PHP和C ++可以在对方之间传递数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的PHP脚本中有很多数学计算,有时可能会很慢。我想知道是否可以将数据从PHP传递到C ++,在C ++中进行计算,然后将结果传回PHP?

I have a lot of mathematical calculations in my PHP script, which can be quite slow at times. I'm wondering if it's possible to pass data from PHP to C++, do the calculations in C++, then pass the results back to PHP?

我对C ++不是很好。

P.S. I'm not very good at C++.

推荐答案

你可以编写一个PHP模块,它是一个C代码,它将一个新的内置的函数暴露给PHP解释器。这将比通过 exec()启动第二个进程快得多。

You can write a PHP module, which is a piece of C code that exposes a new built-in function to the PHP interpreter. This will be much faster than spinning up a second process via exec().

但是,这些模块很难编写,构建工具笨拙,并且在PHP和C之间来回传递参数需要非常小心的进行内存分配和引用计数。此外,C不是C ++,因此您需要一个 extern C 层,以将函数导出到PHP。 (或者用C而不是C ++编写你的扩展。)

However, such modules are difficult to write, the build tools are clumsy, and passing parameters back and forth between PHP and C requires great care around memory allocation and reference counting. Also, C is not C++, so you will need an extern C layer around your C++ code to export functions to PHP. (Or else write your extension in C instead of C++.)

因此,除非你的速度要求非常严格,否则使用 exec proc_open ,并通过文件指针来回传递数据。这就像编写一个从标准输入读取并发射到标准输出的C ++程序。

So unless your speed requirement is really severe, it will be easier to use exec or proc_open and pass data back and forth through file pointers. This would be just like writing a C++ program that reads from standard input and emits to standard output.

这篇关于PHP和C ++可以在对方之间传递数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆