Detailed explanation of examples of built-in functions in the php-X series

黄舟
Release: 2023-03-16 16:44:02
Original
1308 people have browsed it

PHP-X itself is developed based on C++11 and uses cmake for compilation and configuration. The following article mainly introduces you to the relevant information about the use of built-in functions in the PHP-X series of tutorials. The article introduces it in great detail through sample code. Friends in need can refer to it. Let’s take a look together.

Preface

This article mainly introduces you to the use of PHP-X built-in functions, which are often used in PHP extension development. Built-in functions, wrappers in PHP-X, make calling these functions as easy as PHP code.

Friends who are not familiar with php-x can read this article: https://segmentfault.com/a/1190000011111074

echo

If you need to output some content in the extension, you can use the echo function. The use of echo is exactly the same as printf in C language. Please refer to printf related articles for details.

  • In the command line environment (cli), echo will print the screen

  • In php-fpm or apache, echo will output the content to the browser Server client


PHPX_FUNCTION(cpp_test)
{
 echo("a=%d, b=%f, c=%s.\n", args[0].toInt(), args[1].toFloat(), args[2].toCString());
}
Copy after login

var_dump

Develop and debug PHP When programming, we often need to print the values ​​of some variables. PHP provides the var_dump function to print variables. You can also use var_dump in PHP-X. This function accepts a Variant object.


PHPX_FUNCTION(cpp_test)
{
 var_dump(args[0]);
}
Copy after login

include

Include PHP files. Note: If the file does not exist, a fatal error will be thrown. Once loaded correctly, the code in this PHP file will be executed. You can use include to introduce classes and functions implemented by PHP code into extensions.


PHPX_FUNCTION(cpp_test)
{
 include("/data/php/library/Autoloader.php");
}
Copy after login

error

##Print the PHP error log, equivalent to PHP's trigger_error function. This function is very similar to echo, the only difference is that the first parameter is inserted to accept the error level, such as E_ERROR or E_WARNING.


PHPX_FUNCTION(cpp_test)
{
 error(E_ERROR, "error: a=%d, b=%f, c=%s.\n", args[0].toInt(), args[1].toFloat(), args[2].toCString());
}
Copy after login

constant

Get the value of the constant. This function can be used to obtain constants defined by define and class constants defined by const.


PHPX_FUNCTION(cpp_test)
{
 auto a = constant("PHP_VERSION");
 auto b = constant("PDO::VERSION");
}
Copy after login

global

Get the value of the global variable. Includes PHP superglobal variables and global variables declared by other PHP code using the global keyword.


PHPX_FUNCTION(cpp_test)
{
 //相当于 $_GET
 auto a = global("_GET");
 //相当于 global $config
 auto b = global("config");
}
Copy after login

Summary

The above is the detailed content of Detailed explanation of examples of built-in functions in the php-X series. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!