Table of Contents
Preface
Code
PHP extension source code to implement the default_value method
Code description
Getting parameters
zend_parse_parameters
FAST ZPP
Return value
Home Backend Development PHP7 Examples to explain the return value of PHP7 extension development

Examples to explain the return value of PHP7 extension development

Oct 15, 2021 pm 04:32 PM
php7

Preface

This time, we will demonstrate how to accept incoming parameters and output return values ​​in PHP extensions.

<?php
    function default_value ($type, $value = null) {
        if ($type == "int") {
            return $value ?? 0;
        } else if ($type == "bool") {
            return $value ?? false;
        } else if ($type == "str") {
            return is_null($value) ? "" : $value;
        }
        return null;
    }
 
    var_dump(default_value("int"));
    var_dump(default_value("int", 1));
    var_dump(default_value("bool"));
    var_dump(default_value("bool", true));
    var_dump(default_value("str"));
    var_dump(default_value("str", "a"));
    var_dump(default_value("array"));
?>
Copy after login

We will implement the default_value method in the extension. [Recommended: "PHP7 Tutorial"]

Code

PHP extension source code to implement the default_value method

default_value method:

PHP_FUNCTION(default_value)
{
    zend_string     *type;    
    zval            *value = NULL;
 
#ifndef FAST_ZPP
    /* Get function parameters and do error-checking. */
    if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|z", &type, &value) == FAILURE) {
        return;
    }    
#else
    ZEND_PARSE_PARAMETERS_START(1, 2)
        Z_PARAM_STR(type)
        Z_PARAM_OPTIONAL
        Z_PARAM_ZVAL_EX(value, 0, 1)
    ZEND_PARSE_PARAMETERS_END();
#endif
     
    if (ZSTR_LEN(type) == 3 && strncmp(ZSTR_VAL(type), "int", 3) == 0 && value == NULL) {
        RETURN_LONG(0);
    } else if (ZSTR_LEN(type) == 3 && strncmp(ZSTR_VAL(type), "int", 3) == 0 && value != NULL) {
        RETURN_ZVAL(value, 0, 1); 
    } else if (ZSTR_LEN(type) == 4 && strncmp(ZSTR_VAL(type), "bool", 4) == 0 && value == NULL) {
        RETURN_FALSE;
    } else if (ZSTR_LEN(type) == 4 && strncmp(ZSTR_VAL(type), "bool", 4) == 0 && value != NULL) {
        RETURN_ZVAL(value, 0, 1); 
    } else if (ZSTR_LEN(type) == 3 && strncmp(ZSTR_VAL(type), "str", 3) == 0 && value == NULL) {
        RETURN_EMPTY_STRING();
    } else if (ZSTR_LEN(type) == 3 && strncmp(ZSTR_VAL(type), "str", 3) == 0 && value != NULL) {
        RETURN_ZVAL(value, 0, 1); 
    } 
    RETURN_NULL();
}
Copy after login

Code description

Getting parameters

There are two methods of getting parameters in PHP7. zend_parse_parameters and FAST ZPP mode.

zend_parse_parameters

Before PHP7, the zend_parse_parameters function was used to obtain parameters. The function of this function is to convert the incoming parameters into the corresponding types in the PHP kernel, so that they can be used in PHP extensions.
Parameter description:
The first parameter, the number of parameters. Generally just use ZEND_NUM_ARGS(), no need to change.
The second parameter is the format string. The function of this format string is to specify the conversion relationship between the incoming parameters and the PHP core type.

The meaning of S|z in the code is:
S means that the parameter is a string. Convert the incoming parameters to zend_string type.
| indicates that the following parameters are optional. It can be passed on or not.
z indicates that the parameters are of multiple types. Convert the incoming parameters to zval type.

In addition, there are some specifiers that need attention:
! If a null variable in PHP language is received, it is directly converted into NULL in C language instead of encapsulating it into a zval of IS_NULL type.
/ If the passed variable shares a zval with other variables and is not a reference, forced separation is performed. The new zval's is_ref__gc==0, and refcount__gc==1.

More formats The meaning of the string can be viewed on the official website. https://wiki.php.net/rfc/fast_zpp

FAST ZPP

Newly provided in PHP7. It is to improve the performance of parameter parsing. For frequently used methods, it is recommended to use the FAST ZPP method.
Usage:
Start with ZEND_PARSE_PARAMETERS_START(1, 2).
The first parameter indicates the number of parameters that must be passed, and the second parameter indicates the maximum number of parameters that can be passed in.
Ends with ZEND_PARSE_PARAMETERS_END();.
The middle is the parsing of the incoming parameters.
It is worth noting that the macro method of FAST ZPP generally corresponds to the specifier of zend_parse_parameters. For example:
Z_PARAM_OPTIONAL corresponds to |
Z_PARAM_STR corresponds to S
However, the Z_PARAM_ZVAL_EX method is special. It corresponds to two specifiers, namely ! and /. ! Corresponds to the second parameter of the macro method. / corresponds to the third parameter of the macro method. If you want to enable it, just set it to 1.

The corresponding macro method of FAST ZPP can be viewed on the official website https://wiki.php.net/rfc/fast_zpp#proposal

Return value

The return value of the method is Use the macro method starting with RETURN_ to return. Commonly used macro methods are:
RETURN_NULL() Returns null
RETURN_LONG(l) Returns integer type
RETURN_DOUBLE(d) Returns floating point type
RETURN_STR(s) Returns a string. The parameter is a zend_string * pointer
RETURN_STRING(s) Returns a string. The parameter is a char * pointer
RETURN_STRINGL(s, l) returns a string. The second parameter is the string length.
RETURN_EMPTY_STRING() Returns an empty string.
RETURN_ARR(r) Returns an array. Parameters are zend_array* pointers.
RETURN_OBJ(r) Returns an object. Parameters are zend_object* pointers.
RETURN_ZVAL(zv, copy, dtor) returns any type. Parameters are zval* pointers.
RETURN_FALSE Returns false
RETURN_TRUE Returns true

The above is the detailed content of Examples to explain the return value of PHP7 extension development. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install mongo extension in php7.0 How to install mongo extension in php7.0 Nov 21, 2022 am 10:25 AM

How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

How to solve the problem when php7 detects that the tcp port is not working How to solve the problem when php7 detects that the tcp port is not working Mar 22, 2023 am 09:30 AM

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? Apr 02, 2024 pm 07:39 PM

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

PHP Server Environment FAQ Guide: Quickly Solve Common Problems PHP Server Environment FAQ Guide: Quickly Solve Common Problems Apr 09, 2024 pm 01:33 PM

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

How to install and deploy php7.0 How to install and deploy php7.0 Nov 30, 2022 am 09:56 AM

How to install and deploy php7.0: 1. Go to the PHP official website to download the installation version corresponding to the local system; 2. Extract the downloaded zip file to the specified directory; 3. Open the command line window and go to the "E:\php7" directory Just run the "php -v" command.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Apr 01, 2025 pm 03:06 PM

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

Which one is better, php8 or php7? Which one is better, php8 or php7? Nov 16, 2023 pm 03:09 PM

Compared with PHP7, PHP8 has some advantages and improvements in terms of performance, new features and syntax improvements, type system, error handling and extensions. However, choosing which version to use depends on your specific needs and project circumstances. Detailed introduction: 1. Performance improvement, PHP8 introduces the Just-in-Time (JIT) compiler, which can improve the execution speed of the code; 2. New features and syntax improvements, PHP8 supports the declaration of named parameters and optional parameters, making functions Calling is more flexible; anonymous classes, type declarations of properties, etc. are introduced.

See all articles