① 如何用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所在的文件夾下.