{"id":48574,"date":"2024-12-02T13:51:27","date_gmt":"2024-12-02T05:51:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48574\/"},"modified":"2024-12-02T13:51:27","modified_gmt":"2024-12-02T05:51:27","slug":"c-%e6%89%a9%e5%b1%95%e5%92%8c-php-%e5%87%bd%e6%95%b0%e4%b9%8b%e9%97%b4%e7%9a%84%e9%80%9a%e4%bf%a1%e6%9c%ba%e5%88%b6%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48574\/","title":{"rendered":"C \u6269\u5c55\u548c PHP \u51fd\u6570\u4e4b\u95f4\u7684\u901a\u4fe1\u673a\u5236\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>C \u6269\u5c55\u548c PHP \u51fd\u6570\u4e4b\u95f4\u7684\u901a\u4fe1\u673a\u5236\u662f\u4ec0\u4e48\uff1f<\/h1>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aC \u6269\u5c55\u548c PHP \u51fd\u6570\u4e4b\u95f4\u7684\u901a\u4fe1\u673a\u5236\u662f\u4ec0\u4e48\uff1f\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p>C \u6269\u5c55\u4e0e PHP \u51fd\u6570\u901a\u4fe1\u53ef\u901a\u8fc7\u4ee5\u4e0b\u673a\u5236\uff1a1. \u53c2\u6570\u4f20\u9012\uff1aPHP \u51fd\u6570\u53ef\u5411 C \u6269\u5c55\u4f20\u9012\u53c2\u6570\u503c\uff1b2. \u8fd4\u56de\u503c\uff1aC \u6269\u5c55\u53ef\u901a\u8fc7 RETURN_* \u5b8f\u8fd4\u56de PHP \u503c\uff1b3. \u5168\u5c40\u53d8\u91cf\uff1aC \u6269\u5c55\u53ef\u8bbf\u95ee PHP \u4ee3\u7801\u4e2d\u5df2\u58f0\u660e\u7684\u5168\u5c40\u53d8\u91cf\uff1b4. \u56de\u8c03\u51fd\u6570\uff1aC \u6269\u5c55\u53ef\u6ce8\u518c PHP \u56de\u8c03\u51fd\u6570\uff0c\u4ee5\u4fbf\u5728\u7279\u5b9a\u4e8b\u4ef6\u53d1\u751f\u65f6\u8c03\u7528\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u5229\u7528 C \u6269\u5c55\u5b9e\u73b0\u6590\u6ce2\u90a3\u5951\u5e8f\u5217\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730089075671f1073044ba.jpg\" class=\"aligncenter\" title=\"C \u6269\u5c55\u548c PHP \u51fd\u6570\u4e4b\u95f4\u7684\u901a\u4fe1\u673a\u5236\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" alt=\"C \u6269\u5c55\u548c PHP \u51fd\u6570\u4e4b\u95f4\u7684\u901a\u4fe1\u673a\u5236\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>C \u6269\u5c55\u4e0e PHP \u51fd\u6570\u95f4\u7684\u901a\u4fe1\u673a\u5236<\/strong><\/p>\n<p>C \u6269\u5c55\u5728 PHP \u4e2d\u63d0\u4f9b\u4e86\u5bf9\u5e95\u5c42\u529f\u80fd\u7684\u8bbf\u95ee\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e0e PHP \u51fd\u6570\u8fdb\u884c\u901a\u4fe1\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u673a\u5236\u5b9e\u73b0\uff1a<\/p>\n<p><strong>\u53c2\u6570\u4f20\u9012<\/strong><\/p>\n<p>PHP \u51fd\u6570\u53ef\u4ee5\u5c06\u503c\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 C \u6269\u5c55\u3002\u3053\u308c\u3089\u306e\u5024\u306f\u3001\u6b63\u5f0f\u5f15\u6570\u306e\u540d\u524d\u306b\u5f93\u3063\u3066 C \u95a2\u6570\u306e\u5f15\u6570\u306b\u81ea\u52d5\u7684\u306b\u30de\u30c3\u30d7\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre>PHP_FUNCTION(my_extension_function) {\n    long num;\n    char *string;\n\n    if (zend_parse_parameters(ZEND_NUM_ARGS(), \"ls\", &amp;num, &amp;string) == FAILURE) {\n        return;\n    }\n\n    \/\/ Do something with 'num' and 'string'\n}<\/pre>\n<pre>$num = 123;\n$string = \"Hello world\";\nmy_extension_function($num, $string); \/\/ Call the C function<\/pre>\n<p><strong>\u8fd4\u56de\u503c<\/strong><\/p>\n<p>C \u6269\u5c55\u53ef\u4ee5\u901a\u8fc7 <code>RETURN_*<\/code> \u5b8f\u8fd4\u56de PHP \u503c\u3002supported types include <code>ZEND_LONG<\/code>, <code>ZEND_STRING<\/code>, <code>ZEND_BOOL<\/code>, and others.<\/p>\n<pre>PHP_FUNCTION(my_extension_function) {\n    long num = 456;\n\n    RETURN_LONG(num);\n}<\/pre>\n<pre>$result = my_extension_function(); \/\/ Call the C function and get the result<\/pre>\n<p><strong>\u5168\u5c40\u53d8\u91cf<\/strong><\/p>\n<p>C \u6269\u5c55\u53ef\u4ee5\u8bbf\u95ee\u5df2\u5728 PHP \u4ee3\u7801\u4e2d\u58f0\u660e\u7684\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u4e9b\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7 <code>ZEND_GET_MODULE_GLOBALS<\/code> \u5b8f\u83b7\u53d6\u3002<\/p>\n<pre>ZEND_GET_MODULE_GLOBALS(extension_globals);\n\nif (extension_globals-&gt;initialized == 0) {\n    \/\/ Initialize the global variables\n}<\/pre>\n<pre>$global_variable = 123; \/\/ Declare a global variable in PHP\n\n\/\/ Access the global variable in C extension\nif (extension_globals-&gt;initialized &gt; 0) {\n    \/\/ Do something with 'global_variable'\n}<\/pre>\n<p><strong>\u56de\u8c03\u51fd\u6570<\/strong><\/p>\n<p>C \u6269\u5c55\u53ef\u4ee5\u6ce8\u518c PHP \u56de\u8c03\u51fd\u6570\uff0c\u4ee5\u4fbf\u5728\u7279\u5b9a\u4e8b\u4ef6\u53d1\u751f\u65f6\u8c03\u7528\u5b83\u4eec\u3002<\/p>\n<pre>PHP_FUNCTION(register_callback) {\n    zend_fcall_info callback;\n    zend_fcall_info_cache fcc;\n\n    if (zend_fcall_info_init(&amp;callback, ZEND_THIS, NULL, \"my_callback\", 0, NULL, NULL, &amp;fcc) != SUCCESS) {\n        return;\n    }\n\n    zend_register_callback(&amp;callback, &amp;fcc);\n}<\/pre>\n<pre>function my_callback() {\n    \/\/ Called when the event occurs\n}\n\nregister_callback(); \/\/ Call the C function to register the callback<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u5728 C \u6269\u5c55\u4e2d\u5b9e\u73b0\u6590\u6ce2\u90a3\u5951\u5e8f\u5217<\/strong><\/p>\n<p>\u4e0b\u9762\u7684 C \u6269\u5c55\u5b9e\u73b0\u4e86\u6590\u6ce2\u90a3\u5951\u5e8f\u5217\uff1a<\/p>\n<pre>PHP_FUNCTION(fibonacci) {\n    long n;\n\n    if (zend_parse_parameters(ZEND_NUM_ARGS(), \"l\", &amp;n) == FAILURE) {\n        return;\n    }\n\n    RETURN_LONG(fib(n));\n}\n\nstatic long fib(long n) {\n    if (n == 0 || n == 1) {\n        return n;\n    }\n\n    return fib(n - 1) + fib(n - 2);\n}<\/pre>\n<pre>$n = 10;\n$result = fibonacci($n); \/\/ Call the C function<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aC \u6269\u5c55\u548c PHP \u51fd\u6570\u4e4b\u95f4\u7684\u901a\u4fe1\u673a\u5236\u662f\u4ec0\u4e48\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ePHP \u51fd\u6570,C \u6269\u5c55\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C \u6269\u5c55\u548c PHP \u51fd\u6570\u4e4b\u95f4\u7684\u901a\u4fe1\u673a\u5236\u662f\u4ec0\u4e48\uff1f \u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9\u6587\u7ae0\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587\u300aC \u6269\u5c55\u548c PHP \u51fd\u6570\u4e4b\u95f4\u7684\u901a\u4fe1\u673a\u5236\u662f\u4ec0\u4e48\uff1f\u300b\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~ C \u6269\u5c55\u4e0e PHP \u51fd\u6570\u901a\u4fe1\u53ef\u901a\u8fc7\u4ee5\u4e0b\u673a\u5236\uff1a1. \u53c2\u6570\u4f20\u9012\uff1aPHP \u51fd\u6570\u53ef\u5411 C \u6269\u5c55\u4f20\u9012\u53c2\u6570\u503c\uff1b2. \u8fd4\u56de\u503c\uff1aC \u6269\u5c55\u53ef\u901a\u8fc7 RETURN_* \u5b8f\u8fd4\u56de PHP \u503c\uff1b3. \u5168\u5c40\u53d8\u91cf\uff1aC \u6269\u5c55\u53ef\u8bbf\u95ee PHP \u4ee3\u7801\u4e2d\u5df2\u58f0\u660e\u7684\u5168\u5c40\u53d8\u91cf\uff1b4. \u56de\u8c03\u51fd\u6570\uff1aC \u6269\u5c55\u53ef\u6ce8\u518c PHP \u56de\u8c03\u51fd\u6570\uff0c\u4ee5\u4fbf\u5728\u7279\u5b9a\u4e8b\u4ef6\u53d1\u751f\u65f6\u8c03\u7528\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u5229\u7528 C \u6269\u5c55\u5b9e\u73b0\u6590\u6ce2\u90a3\u5951\u5e8f\u5217\u3002 C \u6269\u5c55\u4e0e PHP \u51fd\u6570\u95f4\u7684\u901a\u4fe1\u673a\u5236 C \u6269\u5c55\u5728 PHP \u4e2d\u63d0\u4f9b\u4e86\u5bf9\u5e95\u5c42\u529f\u80fd\u7684\u8bbf\u95ee\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e0e PHP \u51fd\u6570\u8fdb\u884c\u901a\u4fe1\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u673a\u5236\u5b9e\u73b0\uff1a \u53c2\u6570\u4f20\u9012 PHP \u51fd\u6570\u53ef\u4ee5\u5c06\u503c\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 C \u6269\u5c55\u3002\u3053\u308c\u3089\u306e\u5024\u306f\u3001\u6b63\u5f0f\u5f15\u6570\u306e\u540d\u524d\u306b\u5f93\u3063\u3066 C \u95a2\u6570\u306e\u5f15\u6570\u306b\u81ea\u52d5\u7684\u306b\u30de\u30c3\u30d7\u3055\u308c\u307e\u3059\u3002 PHP_FUNCTION(my_extension_function) { long num; char *string; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-48574","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=48574"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48574\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}