Search code examples
phpvisual-c++php-extension

How to pass string,int,char and print them when we are using zend function in php?


I am trying to create php extension in which I pass string, int, char to my .cpp file. My zend function in .cpp file is like this:

#define PHP_COMPILER_ID  "VC9"

#include "php.h"


ZEND_FUNCTION(use_html);

zend_function_entry use_functions[] = 
{
    ZEND_FE(use_html, NULL)
    {NULL, NULL, NULL}
};

zend_module_entry use_html_module_entry = 
{
    STANDARD_MODULE_HEADER,
    "Use Html",
    use_functions,
    NULL, NULL, NULL, NULL, NULL,
    "1.0.0-tutorial",
    STANDARD_MODULE_PROPERTIES
};

ZEND_GET_MODULE(use_html);

ZEND_FUNCTION(use_html)
{
     int useHtml;
     char *ch;
     int a=10,b=120,c;
     if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "/", &useHtml) == FAILURE)
     {
         E_ERROR;
         return;
     }
     c=a+b;
     php_printf("sum is",&useHtml);
     php_printf("This string  is %d :" +  useHtml );
     if(useHtml==1)
     {
         php_printf("\n This string uses <a href='#'>Html</a>");
     }
     else
     {
         php_printf("This string does not Html");
     }

     return;
}

and php code:

<?php

use_html(1);
//use_html("hi"); for string passing
?>

But this does not work. How to pass from php and how to print in zend function?

Error is like this:

( ! ) SCREAM: Error suppression ignored for
( ! ) Warning: use_html() expects parameter 1 to be unknown, integer given in D:\wamp\www\test.php on line 3
Call Stack
#   Time    Memory  Function    Location
1   0.0006  138008  {main}( )   ..\test.php:0
2   0.0006  138352  use_html ( )    ..\test.php:3

Solution

  • On this part:

    if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "/", &useHtml) == FAILURE)
    

    You must specify the expected argument type where "/" is. For accepting every type, you could use "z". But take a look at README.PARAMETER_PARSING_API file, so to access the char*, long, double values, you must to use a macro or just convert the value to string, for such check out the documentation.