views:

31

answers:

1

I have the following C++ function:

void foo() {
    std::cout << "bar" << std::endl;
}

I'm porting this to PHP via SWIG. Everything compiles fine and the extension loads properly. I'm able to call foo() from PHP, but I only see the bar output if I run the PHP script from the command line.

$ php script.php
bar

If I load the script in the browser, nothing appears. Why does it not show bar in this case?

A: 

You can't print to stdout directly. This will of course only work if you're using the CLI SAPI. Use php_printf or any of these:

//Calls php_output_write
#define PHPWRITE(str, str_len)
//Calls php_output_write_unbuffered
#define PHPWRITE_H(str, str_len)
//Idem:
#define PUTC(c)
#define PUTC_H(c)
#define PUTS(str)
#define PUTS_H(str)
int php_write(void *buf, uint size TSRMLS_DC);
int php_printf(const char *format, ...);
int php_output_write(const char *str, size_t len TSRMLS_DC);
int php_output_write_unbuffered(const char *str, size_t len TSRMLS_DC);
//see the rest of main/output.c
Artefacto