views:

667

answers:

1

I haven't used C since the 1980s and 1990s with my own experimentation. I'd like to be able to pick it up again, but this time by building small things in it and then loading it into PHP on Linux.

Does anyone have a very short tutorial for me to make a foo() function in C as a shared object extension loaded in a php.ini? I assume I'll need to use the GCC, but don't know what else I need on my Ubuntu Linux workstation to get this going, or how to compose the files.

Some of the examples I've seen have shown how to do it in C++ or show it as a static extension that must be compiled into PHP. I don't want that -- I want to do it as a C extension, not C++, and load it via php.ini.

I'm thinking of something where I call foo('hello') and it returns 'world' if it sees the incoming string is 'hello'.

For instance, if this was written in 100% PHP, the function might be:

function foo($s) {
  switch ($s)
    case 'hello':
      return 'world';
      break;
    default:
      return $s;
  }
}
+3  A: 

this link might help.... quite details and the example is a Hello World example http://devzone.zend.com/article/1021

Abu Aqil
Thanks, that's a start. I found I needed to only follow the instructions up to "$ php -r 'echo hello_world();'". I also had to know on my Ubuntu that PHP5 loads extensions from /usr/lib/php5/20060613+lfs for some strange reason. And on Ubuntu I had to get PHP Dev on with "sudo apt-get install php-pear php5-dev gcc". Last, I had to put my extension=hello.so entry in both /etc/php5/apache2/php.ini and /etc/php5/cli/php.ini. It returned "Hello, World". Success!However, I still don't know how to pass numeric or string values to my new extension function, though.
Volomike
I finally resolved the query in my last comment. The answer is here: http://devzone.zend.com/node/view/id/1022 -- but in the green code block that contains "zval *zname;". I revised my hello_world function to be just like this one green code block (minus the reference to "greeting") and was able to surmise how to return values based on input values. But then I had to revise this to use RETURN_STRING instead of writing output.
Volomike