Write the first PHP extension to calculate the number of arrays

Source: Internet
Author: User
: This article describes how to write the first PHP extension to calculate the number of arrays. For more information about PHP tutorials, see. Requirement: write the first PHP extension, which contains a function called maxwelldu. maxwelldu can calculate the length of the array (same as count)

Requirements: understand C/C ++ programming and be familiar with PHP programming.

System: CentOS6.5

Environment: LNMP (yum installation)

Take the first step to start writing PHP extensions, will use a tool, and this tool in the PHP source code, so we download a PHP source code, http://php.net/downloads.php

cd ~mkdir softwarecd softwarewget http://cn2.php.net/distributions/php-5.6.11.tar.gztar zxvf php-5.6.11.tar.gzcd php-5.6.11/ext

# Create an extension project. after the extension project is created, an additional sayhello folder will be added to the ext Directory. This folder is our extension project.

./ext_skel --extname=maxwellducd maxwellduvim config.m4

# Enable and remove the dnl and [-- enable-maxwelldu] lines before the PHP_ARG_ENABLE line.

PHP_ARG_ENABLE(maxwelldu, whether to enable maxwelldu support,dnl Make sure that the comment is aligned:[  --enable-maxwelldu           Enable maxwelldu support])

# Add at the end of the file

vim php_maxwelldu.hPHP_FUNCTION(maxwelldu);

# Add at the end of the file

Vim maxwelldu. c "PHP_FUNCTION (maxwelldu) {zval * arr; // declare a variable to accept the array parameter HashTable * arr_hash; // declare a HashTable variable int array_count; // declare an array length variable if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "a", & arr) = FAILURE) {// Determine whether the accepted array is> whether it is an array, and put the value into arr return;} arr_hash = Z_ARRVAL_P (arr ); // Convert the array to HashTable array_count = zend_hash_num_elements (arr_hash); // obtain the total number of elements RETURN_LONG (array_count) through the function provided by zend ); // return the number of elements} "# modify zend_function_entry maxwelldu_functions [] = {. the content of const zend_function_entry maxwelldu_functions [] = {PHP_FE (maxwelldu, NULL) {NULL, NULL, NULL }};"

# Packaging

# Note that the PHP installation methods may vary depending on the php-config directory.

phpize./configure --with-php-c/bin/php-configmakemake testmake install

# At this time, the extension will be automatically placed in the corresponding extension directory

# Modify the php configuration file, just like adding mysql, memcache, and other extensions

# Restart apache or php-fpm

extension=maxwelldu.soservice httpd restartservice php-fpm restart

# View Installed extensions

php -m

# You can view maxwelldu in phpinfo.

# Then you can use it in the PHP script.

 

Reference address:

Http://blog.csdn.net/heiyeshuwu/article/details/3453854

Http://www.360doc.com/content/13/1226/17/14452132_340319333.shtml

Http://www.nowamagic.net/librarys/veda/detail/1467

Http://blog.csdn.net/super_ufo/article/details/3863731

Http://www.phppan.com/2010/02/php-source-12-return_value/

Http://www.ccvita.com/496.html

The above introduces the first PHP extension to calculate the number of arrays, including the content, and hope to be helpful to friends who are interested in the PHP Tutorial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.