php怎么运行存储过程?php存储过程调用实例代码

 更新时间:2017年7月6日 23:50  点击:1364
小编推荐的这篇文章介绍了php存储过程调用实例代码,不知道php怎么运行存储过程的同学可以参考一下,一定会很有收获。

代码如下:

 代码如下复制代码
//比如要调用的存储过程为gxtj(a,b) 
$db=new mysqli("localhost","ssss","aaaaa","bbbb"); 
mysqli_query($db,"SET NAMES utf8"); 
$result=$db->query("call gxtj($year,$jd)"); // gxtj是mysql的存储过程名称 [color=gray][/color] 
while( $row = $result->fetch_array(MYSQLI_ASSOC)) //完成从返回结果集中取出一行 

while ($key=key($row)){ //依次取得字段名 
$value=current($row); //依次取得字段值 

实例一:无参的存储过程

 代码如下复制代码
$conn = mysql_connect('localhost','root','root') or die ("数据连接错误!!!");
mysql_select_db('test',$conn);
$sql = "
create procedure myproce()
begin
INSERT INTO user (id, username, sex) VALUES (NULL, 's', Ɔ');
end; 
";
mysql_query($sql);//创建一个myproce的存储过程

$sql = "call test.myproce();";
mysql_query($sql);//调用myproce的存储过程,则数据库中将增加一条新记录。

 

实例二:传入参数的存储过程

 代码如下复制代码
$sql = "
create procedure myproce2(in score int)
begin
if score >= 60 then
select 'pass'
else
select 'no'
end if;
end; 
";
mysql_query($sql);//创建一个myproce2的存储过程
$sql = "call test.myproce2(70);";
mysql_query($sql);//调用myproce2的存储过程,看不到效果,可以在cmd下看到结果。

实例三:传出参数的存储过程

 代码如下复制代码
$sql = "
create procedure myproce3(out score int)
begin
set score=100;
end; 
";
mysql_query($sql);//创建一个myproce3的存储过程
$sql = "call test.myproce3(@score);";
mysql_query($sql);//调用myproce3的存储过程
$result = mysql_query('select @score;');
$array = mysql_fetch_array($result);
echo '
'print_r($array);

实例四:传出参数的inout存储过程

 代码如下复制代码
$sql = "
create procedure myproce4(inout sexflag int)
begin
SELECT * FROM user WHERE sex = sexflag;
end; 
";
mysql_query($sql);//创建一个myproce4的存储过程
$sql = "set @sexflag = 1";
mysql_query($sql);//设置性别参数为1
$sql = "call test.myproce4(@sexflag);";
mysql_query($sql);//调用myproce4的存储过程,在cmd下面看效果

实例五:使用变量的存储过程

 代码如下复制代码
$sql = "
create procedure myproce5(in a int,in b int)
begin
declare s int default 0;
set s=a+b;
select s;
end; 
";
mysql_query($sql);//创建一个myproce5的存储过程
$sql = "call test.myproce5(4,6);";
mysql_query($sql);//调用myproce5的存储过程,在cmd下面看效果

实例六:case语法

 代码如下复制代码
$sql = "
create procedure myproce6(in score int)
begin
case score
when 60 then select '及格'
when 80 then select '及良好'
when 100 then select '优秀'
else select '未知分数'
end case;
end; 
";
mysql_query($sql);//创建一个myproce6的存储过程
$sql = "call test.myproce6(100);";
mysql_query($sql);//调用myproce6的存储过程,在cmd下面看效果

实例七:循环语句

 代码如下复制代码
$sql = "
create procedure myproce7()
begin
declare i int default 0;
declare j int default 0;
while i<10 do
set j=j+i;
set i=i+1;
end while;
select j;
end; 
";
mysql_query($sql);//创建一个myproce7的存储过程
$sql = "call test.myproce7();";
mysql_query($sql);//调用myproce7的存储过程,在cmd下面看效果

实例八:repeat语句

 代码如下复制代码
$sql = " 
create procedure myproce8()
begin
declare i int default 0;
declare j int default 0;
repeat
set j=j+i;
set i=i+1;
until j>=10
end repeat;
select j;
end; 
";
mysql_query($sql);//创建一个myproce8的存储过程
$sql = "call test.myproce8();";
mysql_query($sql);//调用myproce8的存储过程,在cmd下面看效果

实例九:loop语句

 代码如下复制代码
$sql = "
create procedure myproce9()
begin
declare i int default 0;
declare s int default 0;

loop_label:loop
set s=s+i;
set i=i+1;
if i>=5 then
leave loop_label;
end if;
end loop;
select s;
end; 
";
mysql_query($sql);//创建一个myproce9的存储过程
$sql = "call test.myproce9();";
mysql_query($sql);//调用myproce9的存储过程,在cmd下面看效果

 

实例十:删除存储过程

 

 代码如下复制代码
mysql_query("drop procedure if exists myproce");//删除test的存储过程
实例十:存储过程中的游标
总结中。

 

本文介绍了php运行一个url的教程,不会的同学可以参考一下本文,过程非常简单

PHP:

header('Location: http://www.example.com/');

file_get_contents(http://it.sohu.com/7/1002/17/column20466721_3257.shtml);

php怎么运行c语言程序?本文介绍了利用php调用C语言 扩展PHP的功能的教程,对php初学者来说非常有用,有兴趣的同学快看看吧!

第一步. 生成需要调用的so文件

1.  首先做一个简单的so文件:

/** * hello.c

* To compile, use following commands:

 *   gcc -O -c -fPIC -o hello.o hello.c

  *   gcc -shared -o libhello.so hello.o

*/

int hello_add(int a, int b)

{

    return a + b;

}

然后将它编译成.so文件并放到系统中:

$ gcc -O -c -fPIC -o hello.o hello.c                      // -fPIC:是指生成的动态库与位置无关

$ gcc -shared -o libhello.so hello.o                     // -shared:是指明生成动态链接库

$ su        // 切换成超级用户,此时,需要输入密码。

# cp libhello.so /usr/local/lib      // 把生成的链接库放到指定的地址

# echo /usr/local/lib > /etc/ld.so.conf.d/local.conf       //  把库地址写入到配置文件中

# /sbin/ldconfig                // 用此命令,使刚才写的配置文件生效

2. 写段小程序来验证其正确性:

/**

 * hellotest.c

 * To compile, use following commands:

*   gcc -o hellotest -lhello hellotest.c

*/

#include

int main()

{

    int a = 3, b = 4;

    printf("%d + %d = %d", a, b, hello_add(a,b));

    return 0;

}

编译并执行:

$ gcc -o hellotest -lhello hellotest.c                // 编译测试文件,生成测试程序

$ ./hellotest           // 运行测试程序

第二步. 制作PHP模块(外部模块)

请确保你已安装 PHP及APACHE服务器。

$ cd php-5.2.3/ext

1. 然后通过下面的命令用ext_skel脚本建立一个名为 hello 的模块:

$ ./ext_skel --extname=hello

2. 执行该命令之后它会提示你应当用什么命令来编译模块,可惜那是将模块集成到php内部的编译方法。

如果要编译成可动态加载的 php_hello.so,方法要更为简单。

$ cd hello

 首先编辑 config.m4 文件,去掉第16行和第18行的注释(注释符号为 dnl 。)

16:  PHP_ARG_ENABLE(hello, whether to enable hello support,

17:  dnl Make sure that the comment is aligned:

18:  [  --enable-hello           Enable hello support])

3. 然后执行 phpize 程序,生成configure脚本:

$ phpize

 该程序在ubuntu的php5-dev包中

4. 打开 php_hello.h,在 PHP_FUNCTION(confirm_hello_compiled); 之下加入函数声明:

PHP_FUNCTION(confirm_hello_compiled);   /* For testing, remove later. */

PHP_FUNCTION(hello_add);

5. 打开 hello.c,在 PHP_FE(confirm_hello_compiled, NULL) 下方加入以下内容。

zend_function_entry hello_functions[] = {

    PHP_FE(confirm_hello_compiled,  NULL)       /* For testing, remove later. */

    PHP_FE(hello_add,   NULL)       /* For testing, remove later. */

    {NULL, NULL, NULL}  /* Must be the last line in hello_functions[] */};

    然后在 hello.c 的最末尾书写hello_add函数的内容: 

PHP_FUNCTION(hello_add)

{

    long int a, b;

    long int result;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) {

        return;

    }

    result = hello_add(a, b);

    RETURN_LONG(result);}保存退出,编译并安装:

6. $ ./configure

$ make LDFLAGS=-lhello

$ sudo make install

Installing shared extensions:     /usr/lib/php5/20060613+lfs/$ su# cp modules/hello.so/usr/lib/php/modules

luther@gliethttp:~$ sudo vim /etc/php5/apache2/php.ini;

enable_dl = Off;允许dl()动态加载so扩展功能enable_dl = On

luther@gliethttp:~$ sudo service apache2 reload    然后在 /var/www/html 下建立一个 hello.php 文件,内容如下:

<?php

    dl("hello.so");

    echo hello_add(3, 4);

?>

然后在浏览器中打开hello.php文件,如果显示7,则说明函数调用成功了。

第三步. 制作PHP模块(内部模块)

另外可以在apache重启的时候让我们的so库直接动态编译进php5,就像linux的insmod hello.ko模块一样,不用dl加载也不用重新编译php,就可以直接使用so的函数了,步骤如下:

luther@gliethttp:~$ sudo vim /etc/php5/apache2/php.ini
enable_dl = Off
extension=hello.so
luther@gliethttp:~$ sudo service apache2 restart

不能reload而必须restart apache,这样so就像insmod hello.ko一样被融到了php5内核,然后代码就可以忽略掉dl("hello.so");了,

[注意,这种方式只适合hello.so库内所有功能代码已经全部调试ok,如果还处在调试期间,那么需要采用上面的dl强制加载的方式]

代码如下:

<?php

    echo hello_add(3, 4);

?>,

     但是该功能不太适合调试,因为每次修改hello.so中代码的话,都需要让service apacherestart重启才能让php5内核再次加载新的hello.so扩展.可以这样定义hello.so的实现,这样每次执行.php网页,都会在/var/www/下建立一个文件夹,所以php扩展实现了

一,搭建php环境
下载php 5.2.6 源码 并解压
编译安装,搭建php环境
二,创建扩展项目
进入源码目录
cd php5.2.6/ext/
./ext_skel --extname=my_ext
创建名字为my_ext的项目,最终会生成my_ext.so
三,更改配置和程序
$ vi ext/my_ext/config.m4
根据你自己的选择将
dnl PHP_ARG_WITH(my_ext, for my_ext support,
dnl Make sure that the comment is aligned:
dnl [ --with-my_ext Include my_ext support])
修改成
PHP_ARG_WITH(my_ext, for my_ext support,
Make sure that the comment is aligned:
[ --with-my_ext Include my_ext support])
或者将
dnl PHP_ARG_ENABLE(my_ext, whether to enable my_ext support,
dnl Make sure that the comment is aligned:
dnl [ --enable-my_ext Enable my_ext support])
修改成
PHP_ARG_ENABLE(my_ext, whether to enable my_ext support,
Make sure that the comment is aligned:
[ --enable-my_ext Enable my_ext support])
$ vi ext/my_ext/php_my_ext.h

PHP_FUNCTION(confirm_my_ext_compiled); /* For testing, remove later. */
更改为
PHP_FUNCTION(say_hello);
$ vi ext/my_ext/my_ext.c

zend_function_entry php5cpp_functions[] = {
PHP_FE(confirm_my_ext_compiled, NULL) /* For testing, remove later. */
{NULL, NULL, NULL} /* Must be the last line in php5cpp_functions[] */
};
更改为
zend_function_entry php5cpp_functions[] = {
PHP_FE(say_hello, NULL)
{NULL, NULL, NULL} /* Must be the last line in php5cpp_functions[] */
};
在最后添加:
PHP_FUNCTION(say_hello)
{
zend_printf("hello world\n");
}
四,编译
$ cd my_ext
$ /usr/local/php/bin/phpize
ps: 如果出现:Cannot find autoconf.……的错误信息,则需要安装 autoconf (安装过程略)
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make
这时会编译出 my_ext/modules/my_ext.so
五,配置php.ini
将my_ext.so放入/usr/local/php/ext/目录
$ vi php.ini

修改添加如下:
extension_dir = '/usr/local/php/ext/'
extension=my_ext.so
六,测试
$ vi test.php
<?php
say_hello();
?>
$ /usr/local/php/bin/php test.php
hello world.
则大功告成

下面我来讲讲如何作一个php的扩展
首先要有一个搭建好的php环境
我把php的安装在了/usr/local/php当然也通过
php的一个配置php.ini的路径但是要注意了
用这种方法安装的php扩展不能实现
我们在php安装以后的/usr/local/php/bin目录
找到这个文件phpize稍后我们将用到他
他就是个shell脚本你可以用vi phpize来查看他的内容
但是你要注意了这个脚本不是在哪里都可以应用的
[root@ns root]# phpize
Cannot find config.m4.
Make sure that you run '/usr/local/bin/phpize' in the top level source directory of the module

[root@ns root]# phpize
Cannot find config.m4.
Make sure that you run '/usr/local/bin/phpize' in the top level source directory of the module
你会看到这两种结果实际上你查看了这个脚本
很轻松的就会发现是怎么来处理的
你的模扩展的时候最好
放在/usr/local/src/php-4.3.5/ext下
来执行他你在这里也可以这样/usr/local/php/bin/phpize来执行也可以
phpize来执行

我们在/usr/local/src/php-4.3.5/ext下找到这个工具
来建立一个php扩展的一个框架
[root@ns ext]#cd /usr/local/src/php-4.3.5/ext/
[root@ns ext]# ./ext_skel --extname=jinzhesheng_module
Creating directory jinzhesheng_module
Creating basic files: config.m4 .cvsignore jinzhesheng_module.cphp_jinzhesheng_module.h CREDITS EXPERIMENTAL tests/001.phptjinzhesheng_module.php [done].

To use your new extension, you will have to execute the following steps:

1.  $ cd ..
2.  $ vi ext/jinzhesheng_module/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-jinzhesheng_module
5.  $ make
6.  $ ./php -f ext/jinzhesheng_module/jinzhesheng_module.php
7.  $ vi ext/jinzhesheng_module/jinzhesheng_module.c
8.  $ make
执行了这个步骤以后你会看到这样的结果
Repeat steps 3-6 until you are satisfied with ext/jinzhesheng_module/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.
这样以后我们会在这个目录下生成一个目录叫jinzhesheng_module
进入这里面我们看看
[root@ns ext]# cd jinzhesheng_module/
[root@ns jinzhesheng_module]# ls
config.m4  EXPERIMENTAL          jinzhesheng_module.php    tests
CREDITS    jinzhesheng_module.c  php_jinzhesheng_module.h

然后我们要修改文件顺序是
configue.m4
jinzhesheng_module.c
php_jinzhesheng_module.h

使用文本编辑器打开config.m4文件,文件内容大致如下:

dnl $Id$d

dnl config.m4 for extension my_module

dnl don't forget to callPHP_EXTENSION(my_module)

dnl Comments in this file start with the string 'dnl'.

dnl Remove where necessary. This file will not work

dnl without editing.

dnl If your extension references something external, use with:

dnl PHP_ARG_WITH(my_module, for my_module support,

dnl Make sure that the comment is aligned:

dnl [  --with-my_module             Include my_module support])

dnl Otherwise use enable:

dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,

dnl Make sure that the comment is aligned:

dnl [  --enable-my_module           Enable my_module support])

if test "$PHP_MY_MODULE" != "no"; then

  dnl If you will not be testing anything external, like existence of

  dnl headers, libraries or functions in them, just uncomment the

  dnl following line and you are ready to go.

  dnl Write more examples of tests here...

  PHP_EXTENSION(my_module, $ext_shared)

Fi

根据你自己的选择将

dnl PHP_ARG_WITH(my_module, for my_module support,

dnl Make sure that the comment is aligned:

dnl [  --with-my_module             Include my_module support])

修改成

PHP_ARG_WITH(my_module, for my_module support,

Make sure that the comment is aligned:

[  --with-my_module             Include my_module support])

或者将

dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,

dnl Make sure that the comment is aligned:

dnl [  --enable-my_module           Enable my_module support])

修改成

PHP_ARG_ENABLE(my_module, whether to enable my_module support,

Make sure that the comment is aligned:

[  --enable-my_module           Enable my_module support])
我这里用了后者
然后保存退出
然后在编辑
Vi my_module.c
将文件其中的下列代码进行修改

/* Every user visible function must have an entry in my_module_functions[].

*/

function_entry my_module_functions[] = {

        PHP_FE(say_hello,       NULL)  /* ?添加着一行代码 */

        PHP_FE(confirm_my_module_compiled,      NULL) /* For testing, remove later. */

        {NULL, NULL, NULL}      /* Must be the last line in my_module_functions[] */

};

在文件的最后添加下列代码

PHP_FUNCTION(say_hello)

{

        zend_printf("hello world\n");

}

保存文件退出

然后我们就可以在这个目录下使用上面的命令了
/usr/local/php/bin/phpize
执行以后会看到下面的
[root@ns jinzhesheng_module]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20020918
Zend Module Api No:      20020429
Zend Extension Api No:   20050606
[root@ns jinzhesheng_module]#
然后执行./configure --enable-jinzhesheng_module --with-apxs=/usr/local/apache/bin/apxs --with-php-config=/usr/local/php/bin/php-config
我们在安装以后的php的bin目录下的可以找到这个文件的
php-config 和phpize
这一步骤一定要注意你的apache的apxs放在哪里了
然后执行make
你会看到出现错误了你重新定义了函数我们前面的
这个你在回头改一下这个文件把原来的函数删除掉在生成的文件里面会有同样的函数
你在加入你的代码
就可以通过了
这个时候会在当前的目录下生成一个目录叫modules他的下面就放着你要的
jinzhesheng_module.so文件
cp modules/jinzhesheng_module.so /usr/local/php/ext/
这里需要你先设置你的php的扩展目录的在
在php.ini里面
通过extension_dir
最后一不是你在php.ini文件中打开这个扩展
extension=jinzhesheng_module.so
然后
重新起动apache
用phpinfo来察看一下ok了

这篇文章主要介绍了PHP中余数、取余的妙用,这个是在WEB开发中一个经常使用的小技巧,不管是在表格、ul、li或者是换行等HTML处理中,需要隔几行换行,隔几行加入ul或li的结束标记等时候,特别的有用,有需要的同学可以参考一下。
 代码如下复制代码
<?php

  

$ary=array("name","egineer","sonny","tonny","pingk","apple","phone","clone","pink","colle");

foreach($aryas$key=>$value){

$key=$key+1;

if($key%2==1){echo'<li>';}

  ?>

 <span><?phpecho$key.":".$value;?></span>

  

<?php

 if($key%2==0){echo'</li>';}

};

 ?>

second_one

 代码如下复制代码

<?php

  

$ary=array("name","egineer","sonny","tonny","pingk","apple","phone","clone","pink","colle");

foreach($aryas$key=>$value){

$key=$key;

if($key%3==0){echo'<li>';}

  ?>

 <spanclass="<?php echo($key%3); ?>span"><?phpecho$key.":".$value;?></span>

  

<?php

 if($key%3==2){echo'</li>';}

};

 ?>


大家可以运行以后查看结果并分析其巧妙之处~

在php程序中有事会需要用到C代码,这篇文章着重介绍一下用C写php扩展的方法,而且不需要重新编译php。有需要的同学可以参考一下。

在php程序中需要用到C代码,应该是下面两种情况:

1 已有C代码,在php程序中想直接用
2 由于php的性能问题,需要用C来实现部分功能

针对第一种情况,最合适的方法是用system调用,把现有C代码写成一个独立的程序。参数通过命令行或者标准输入传入,结果从标准输出读出。其次,稍麻烦一点的方法是C代码写成一个daemon,php程序用socket来和它进行通讯。

重点讲讲第二种情况,虽然沿用system调用的方法也可以,但是想想你的目的是优化性能,那么频繁的起这么多进程,当然会让性能下降。而写daemon的方法固然可行,可是繁琐了很多。

我的简单测试,同样一个算法,用C来写比用php效率能提高500倍。而用php扩展的方式,也能提高90多倍(其中的性能损失在了参数传递上了吧,我猜)。

所以有些时候php扩展就是我们的最佳选择了。

这里我着重介绍一下用C写php扩展的方法,而且不需要重新编译php。

首先,找到一个php的源码,php4或者php5版本的都可以,与你目标平台的php版本没有关系。

在源码的ext目录下可以找到名为ext_skel的脚本(windows平台使用ext_skel_win32.php)
在这个目录下执行./ext_skel --extname=hello(我用hello作为例子)
这时生成了一个目录 hello,目录下有几个文件,你只需要关心这三个:config.m4 hello.c php_hello.h

把这个目录拷备到任何你希望的地方,cd进去,依次执行
phpize
/configure
make
什么也没发生,对吧?
这是因为漏了一步,打开config.m4,找到下面
dnl If your extension references something external, use with:
..
dnl Otherwise use enable:
..
这是让你选择你的扩展使用with还是enable,我们用with吧。把with那一部分取消注释。
如果你和我一样使用vim编辑器,你就会很容易发现dnl三个字母原来是表示注释的呀(这是因为vim默认带了各种文件格式的语法着色包)

我们修改了config.m4后,继续
phpize
/configure
make
这时,modules下面会生成hello.so和hello.la文件。一个是动态库,一个是静态库。

你的php扩展已经做好了,尽管它还没有实现你要的功能,我先说说怎么使用这个扩展吧!ext_skel为你生成了一个hello.php里面有调用示例,但是那个例子需要你把hello.so拷贝到php的扩展目录中去,我们只想实现自己的功能,不想打造山寨版php,改用我下面的方法来加载吧:

 代码如下 复制代码
if(!extension_loaded("hello")) {
        dl_local("hello.so");
}
function dl_local( $extensionFile ) {
        //make sure that we are ABLE to load libraries06.        if( !(bool)ini_get( "enable_dl" ) || (bool)ini_get( "safe_mode" ) ) {
                die( "dh_local(): Loading extensions is not permitted./n" );
        }
        //check to make sure the file exists11.        if( !file_exists(dirname(__FILE__) . "/". $extensionFile ) ) {
                die( "dl_local(): File '$extensionFile' does not exist./n" );
        }
        //check the file permissions16.        if( !is_executable(dirname(__FILE__) . "/". $extensionFile ) ) {
                die( "dl_local(): File '$extensionFile' is not executable./n" );
        }
        //we figure out the path21.        $currentDir = dirname(__FILE__) . "/";
        $currentExtPath = ini_get( "extension_dir" );
        $subDirs = preg_match_all( "////" , $currentExtPath , $matches );
        unset( $matches );
        //lets make sure we extracted a valid extension path27.        if( !(bool)$subDirs ) {
                die( "dl_local(): Could not determine a valid extension path [extension_dir]./n" );
        }
        $extPathLastChar = strlen( $currentExtPath ) - 1;
        if( $extPathLastChar == strrpos( $currentExtPath , "/" ) ) {
                $subDirs--;
        }
        $backDirStr = ""; 
        for( $i = 1; $i <= $subDirs; $i++ ) {
                $backDirStr .= "..";
                if( $i != $subDirs ) {
                  $backDirStr .= "/";
                }
        }
        //construct the final path to load46.        $finalExtPath = $backDirStr . $currentDir . $extensionFile;
        //now we execute dl() to actually load the module49.        if( !dl( $finalExtPath ) ) {
                die();
        }
        //if the module was loaded correctly, we must bow grab the module name54.        $loadedExtensions = get_loaded_extensions();
        $thisExtName = $loadedExtensions[ sizeof( $loadedExtensions ) - 1 ];
        //lastly, we return the extension name58.        return $thisExtName;
}//end dl_local()

这样的好处是你的php扩展可以随你的php代码走,绿色扩展。

随后一个让人关心的问题是,如何添加函数、实现参数传递和返回值

添加函数步骤如下:

 代码如下 复制代码

php_hello.h:
PHP_FUNCTION(confirm_hello_compiled);// 括号里面填写函数名

hello.c
zend_function_entry hello_functions[] = {
    PHP_FE(confirm_hello_compiled,  NULL)       /* 这里添加一行 */
    {NULL, NULL, NULL}  /* Must be the last line in hello_functions[] */
};
PHP_FUNCTION(confirm_hello_compiled) 
{// 这里写函数体
}

要实现的函数原型其实都一个样,用宏PHP_FUNCTION来包装了一下,另外呢,在hello_functions里面添加了一行信息,表示你这个模块中有这个函数了。

那么都是一样的函数原型,如何区分返回值与参数呢?
我给一个例子:

 代码如下 复制代码
PHP_FUNCTION(hello_strdiff)
{
    char *r1 = NULL, *r2 = NULL;
    int n = 0, m = 0;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &r1, &n, &r2, &m) == FAILURE) {
        return;
    }
    while(n && m && *r1 == *r2) {
        r1++;
        r2++;
        n--;
        m--;
    }
    if(n == 0) RETURN_LONG(m);
    if(m == 0) RETURN_LONG(n);
    int d[n+1][m+1];
    int cost;
    int i,j;
    for(i = 0; i <= n; i++) d[i][0] = i;
    for(j = 0; j <= m; j++) d[0][j] = j;
    for(i = 1; i <= n; i++) {
        for(j = 1; j <= m; j++) {
            if(r1[i-1] == r2[j-1]) cost = 0;
            else cost = 1;
            int a = MIN(d[i-1][j]+1,d[i][j-1]+1);
            a = MIN(a, d[i-1][j-1]+cost);
            d[i][j] = a;
        }
    }
    RETURN_LONG(d[n][m]);
}

这是一个求两个字符串差异度的算法,输入参数两个字符串,返回整型。
参数的传递看这里
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &r1, &n, &r2, &m)

把这个当成是scanf来理解好了。
类型说明见下表:

Booleanbzend_bool
Longllong
Doubleddouble
Stringschar*, int
Resourcerzval*
Arrayazval*
Objectozval*
zvalzzval*

如果想实现可选参数的话,例如一个字符串,一个浮点,再加一个可选的bool型,可以用"sd|b"来表示。
和scanf有一点不同的是,对于字符串,你要提供两个变量来存储,一个是char *,存字符串的地址,一个int,来存字符串的长度。这样有必要的时候,你可以安全的处理二进制数据。

那么返回值怎么办呢?
使用下面一组宏来表示:

RETURN_STRING
RETURN_LONG
RETURN_DOUBLE
RETURN_BOOL
RETURN_NULL

注意RETURN_STRING有两个参数
当你需要复制一份字符串时使用
RETURN_STRING("Hello World", 1);

否则使用
RETURN_STRING(str, 0);

这里涉及到了模块中内存的分配,当你申请的内存需要php程序中去释放的话,请参照如下表

TraditionalNon-PersistentPersistent
malloc(count)
           calloc(count, num)
emalloc(count)
           ecalloc(count, num)
pemalloc(count, 1)*
           pecalloc(count, num, 1)
strdup(str)
           strndup(str, len)
estrdup(str)
           estrndup(str, len)
pestrdup(str, 1)
           pemalloc() & memcpy()
free(ptr)efree(ptr)pefree(ptr, 1)
realloc(ptr, newsize)erealloc(ptr, newsize)perealloc(ptr, newsize, 1)
malloc(count * num + extr)**safe_emalloc(count, num, extr)safe_pemalloc(count, num, extr)

一般我们使用Non-Persistent中列出的这些好了。

基本上就是这样,可以开始写一个php的扩展了。
从我目前的应用来看,能操纵字符串就够用了,所以我就只能介绍这么多了。

[!--infotagslink--]

相关文章

  • 金额阿拉伯数字转换为中文的存储过程

    Create Procedure AtoC @ChangeMoney Money as Set Nocount ON Declare @String1 char(20) Declare @String2 char(30) ...2016-11-25
  • PHP操作MSSQL存储过程修改用户密码

    存储过程在数据库的应用中我们用到的非常的多了,下面我们来看一篇关于PHP操作MSSQL存储过程修改用户密码的例子,具体的如下所示。 mssql2008 存储过程 下面可以直接...2016-11-25
  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • Powershell实现编写和运行脚本

    本文为那些对学习 Windows PowerShell 命令行和脚本编写环境感兴趣的系统管理员提供了资源。也请告诉我们本网站如何才能对您更有用处。...2020-06-30
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • js识别uc浏览器的代码

    其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
  • JS实现双击屏幕滚动效果代码

    本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • PHP开发微信支付的代码分享

    微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31
  • C#隐式运行CMD命令(隐藏命令窗口)

    这篇文章主要介绍了C#隐式运行CMD命令(隐藏命令窗口),本文实现在winform窗口中运行CMD命令,需要的朋友可以参考下...2020-06-25
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • MySql存储过程之逻辑判断和条件控制

    具体详情请看下文小编给大家带来的知识点。同编写程序类似,存储过程中也有对应的条件判断,功能类似于if、switch。在MySql里面对应的是IF和CASE1、IF判断IF判断的格式是这样的:IF expression THEN commands [ELSEIF ex...2015-10-21
  • 解决Pycharm 运行后没有输出的问题

    这篇文章主要介绍了解决Pycharm 运行后没有输出的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-06
  • php怎么用拼音 简单的php中文转拼音的实现代码

    小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06