導航:首頁 > 編程語言 > c運行php

c運行php

發布時間:2025-06-03 06:06:39

① 如何用c語言在windows平台上開發php extension

何使用C語言發PHP擴展

函數功能:php面整數符號數其內部實現其實longunsigned long於32位機器說php能表示整數2^31-1般應用碰於2^31-1於2^32數能用字元串表示於mixed int_ext(string in)說字元串in表示整數於2^31-1返整數於返字元串

發擴展步驟:(首先需要載php源碼載php-5.3.14)

1建立擴展骨架

[plain] view plainprint?
01.cd php-5.3.14/ext
02../ext_skel --extname=int_ext
cd php-5.3.14/ext
./ext_skel --extname=int_ext
2修改編譯參數

[plain] view plainprint?
01.cd php-5.3.14/ext/int_ext
02.vi config.m4
cd php-5.3.14/ext/int_ext
vi config.m4掉 PHP_ARG_ENABLE(int_ext, whether to enable int_ext support
[ --enable-int_ext Enable int_ext support]) 兩行前面dnl 修改:

[plain] view plainprint?
01.1. dnl Otherwise use enable:
02.2. PHP_ARG_ENABLE(int_ext, whether to enable int_ext support,
03.3. dnl Make sure that the comment is aligned:
04.4. [ --enable-int_ext Enable int_ext support])
1. dnl Otherwise use enable:
2. PHP_ARG_ENABLE(int_ext, whether to enable int_ext support,
3. dnl Make sure that the comment is aligned:
4. [ --enable-int_ext Enable int_ext support])
3編寫C代碼

[plain] view plainprint?
01.cd php-5.3.14/ext/int_ext
02.vi php_int_ext.h
03.# PHP_FUNCTION(confirm_int_ext_compiled); 面新增行 PHP_FUNCTION(int_ext);
cd php-5.3.14/ext/int_ext
vi php_int_ext.h
# PHP_FUNCTION(confirm_int_ext_compiled); 面新增行 PHP_FUNCTION(int_ext);[plain] view plainprint?
01.cd php-5.3.14/ext/int_ext
02.vi int_ext.c
03.#PHP_FE(confirm_int_ext_compiled, NULL) 面添加 PHP_FE(int_ext, NULL)添加:
04.1. zend_function_entry int_ext_functions[] = {
05.2. PHP_FE(confirm_int_ext_compiled, NULL) /* For testing, remove later. */
06.3. PHP_FE(int_ext, NULL) /* For testing, remove later. */
07.4. {NULL, NULL, NULL} /* Must be the last line in int_ext_functions[] */
08.5. };
cd php-5.3.14/ext/int_ext
vi int_ext.c
#PHP_FE(confirm_int_ext_compiled, NULL) 面添加 PHP_FE(int_ext, NULL)添加:
1. zend_function_entry int_ext_functions[] = {
2. PHP_FE(confirm_int_ext_compiled, NULL) /* For testing, remove later. */
3. PHP_FE(int_ext, NULL) /* For testing, remove later. */
4. {NULL, NULL, NULL} /* Must be the last line in int_ext_functions[] */
5. };
核代碼:

[plain] view plainprint?
01.PHP_FUNCTION(int_ext)
02.{
03. char * str = NULL;
04. int str_len;
05. int argc = ZEND_NUM_ARGS();
06. if(zend_parse_parameters(argc TSRMLS_CC,"s",&str,&str_len) == FAILURE)
07. return ;
08. char * result;
09. int result_length = str_len;
10. result = (char *) emalloc(result_length + 1);
11. memcpy(result,str,result_length);
12. unsigned long result_num = strtoul(result, NULL, 10);
13. int sizeoflong sizeof(long);
14. unsigned long max_long = 1 << (sizeoflong * 8 -1);
15. if(result_num < max_long)
16. {
17. RETURN_LONG(result_num);
18. }
19. else
20. {
21. RESULT_STRINGL(result, result_length, 0);
22. }
23.}
PHP_FUNCTION(int_ext)
{
char * str = NULL;
int str_len;
int argc = ZEND_NUM_ARGS();
if(zend_parse_parameters(argc TSRMLS_CC,"s",&str,&str_len) == FAILURE)
return ;
char * result;
int result_length = str_len;
result = (char *) emalloc(result_length + 1);
memcpy(result,str,result_length);
unsigned long result_num = strtoul(result, NULL, 10);
int sizeoflong sizeof(long);
unsigned long max_long = 1 << (sizeoflong * 8 -1);
if(result_num < max_long)
{
RETURN_LONG(result_num);
}
else
{
RESULT_STRINGL(result, result_length, 0);
}
}

4編譯

[plain] view plainprint?
01.cd php-5.3.14/ext/int_ext
02./usr/local/php/bin/pphpize
03../configure --with-php-config=/usr/local/php/bin/php-config
04.make
05.make install
cd php-5.3.14/ext/int_ext
/usr/local/php/bin/pphpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
產so文件: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/int_ext.so
修改php.ini 添加擴展extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"

[int_ext]

extension = int_ext.so

5測試

[plain] view plainprint?
01.$a = int_ext("12345678900");
02.var_mp($a);
03.$a = int_ext("123456789");
04.var_mp($a);
$a = int_ext("12345678900");
var_mp($a);
$a = int_ext("123456789");
var_mp($a);
結輸:

[plain] view plainprint?
01.string(11) "12345678900"
02.int(123456789)

② 怎麼在本地運行PHP!

本地運行PHP的方法如下:

1、首先需要去Phpstudy官網下載相應系統的軟體包,這里我默認使用的是XP系統(WIN7系統步驟相同),選擇軟體包進行下載安裝即可。

③ 用PHP編寫的一行行代碼 是怎麼在伺服器端運行的


從圖上可以看到,PHP實現了一個典型的動態語言執行過程:拿到一段代碼後,經過詞法解析、語法解析等階段後,源程序會被翻譯成一個個指令 (opcodes),然後ZEND虛擬機順次執行這些指令完成操作。PHP本身是用C實現的,因此最終調用的也都是C的函數,實際上,我們可以把PHP看 做是一個C開發的軟體。

PHP的執行的核心是翻譯出來的一條一條指令,也即opcode。

Opcode是PHP程序執行的最基本單位。一個opcode由兩個參數(op1,op2)、返回值和處理函數組成。PHP程序最終被翻譯為一組opcode處理函數的順序執行。

常見的幾個處理函數:

1 ZEND_ASSIGN_SPEC_CV_CV_HANDLER : 變數分配 ($a=$b)

2 ZEND_DO_FCALL_BY_NAME_SPEC_HANDLER:函數調用

3 ZEND_CONCAT_SPEC_CV_CV_HANDLER:字元串拼接 $a.$b

4 ZEND_ADD_SPEC_CV_CONST_HANDLER: 加法運算 $a+2

5 ZEND_IS_EQUAL_SPEC_CV_CONST:判斷相等 $a==1

6 ZEND_IS_IDENTICAL_SPEC_CV_CONST:判斷相等 $a===1

④ windows下用命令符運行php腳本,提示錯誤怎麼辦

可能有兩個原因,一個是文件格式的問題,另一個就是環境變數中的PATH變數沒有設置好,或者你可以嘗試著把php文件移動到php5即php.exe所在的文件夾下.

閱讀全文

與c運行php相關的資料

熱點內容
人工智慧演算法是多少位進制 瀏覽:920
發出命令的人 瀏覽:515
androidmainxml 瀏覽:109
des可逆加密演算法 瀏覽:249
aix查看系統信息常用命令 瀏覽:154
phpmemcache實例 瀏覽:249
爆槍源碼 瀏覽:567
編程大佬能記住所有代碼 瀏覽:859
如何批量注冊國家反詐app 瀏覽:527
實現ping命令 瀏覽:19
cmd進入管理員命令 瀏覽:479
pdf在線編輯修改 瀏覽:967
文件夾是只讀是什麼意思 瀏覽:88
伺服器如何知道訪問域名 瀏覽:319
java網路編程實驗總結 瀏覽:86
linux下dns伺服器配置 瀏覽:707
我的命令是絕對的 瀏覽:932
助飛器app在哪裡下 瀏覽:64
無廣告win10解壓縮 瀏覽:476
台灣的伺服器怎麼選雲伺服器 瀏覽:813