Reputation: 9
Now I have one question about how to view the source code of sprintf
php funciton. Because I want to know where and how is the function executes.
I could find some function, such as round function in the math.c by grep -r 'PHP_FUNCTION(round)'
, but sprintf is not similar with round function.
Upvotes: 0
Views: 969
Reputation: 72206
PHP implementation uses the php_formatted_print()
internal function that does the processing for all the *printf()
PHP functions (printf()
, sprintf()
, fprintf()
, vprintf()
, vsprintf()
, vfprintf()
). They all do similar processing, only the destination of the output differs.
The C function user_sprintf()
(named this way to avoid conflicting with the sprintf()
function provided by the standard C library) is declared as the implementation of the sprintf()
PHP function.
Its code is very simple (the hard lifting is performed by php_formatted_print()
):
/* {{{ proto string sprintf(string format [, mixed arg1 [, mixed ...]])
Return a formatted string */
PHP_FUNCTION(user_sprintf)
{
zend_string *result;
if ((result=php_formatted_print(execute_data, 0, 0))==NULL) {
RETURN_FALSE;
}
RETVAL_STR(result);
}
/* }}} */
There are similar functions that implement the other *printf()
PHP functions. They differ in the way they handle the value returned by php_formatted_print()
.
Upvotes: 1
Reputation: 1890
There are 2 good ways to view the PHP source.
First and easiest is by using your IDE. For example in Netbeans ctrl+right click
on any function will take you to it's source. This method is without a doubt the fastest, and when writing code it's the easiest place to goto to understand how the function works.
However you won't always get the complete source. Using sprintf
as an example you simply get the following.
/**
* (PHP 4, PHP 5, PHP 7)<br/>
* Return a formatted string
* @link http://php.net/manual/en/function.sprintf.php
* @param string $format <p>
* The format string is composed of zero or more directives:
* ordinary characters (excluding %) that are
* copied directly to the result, and conversion
* specifications, each of which results in fetching its
* own parameter. This applies to both <b>sprintf</b>
* and <b>printf</b>.
* </p>
* <p>
* Each conversion specification consists of a percent sign
* (%), followed by one or more of these
* elements, in order:
* An optional sign specifier that forces a sign
* (- or +) to be used on a number. By default, only the - sign is used
* on a number if it's negative. This specifier forces positive numbers
* to have the + sign attached as well, and was added in PHP 4.3.0.
* @param mixed $args [optional]
* @param mixed $_ [optional]
* @return string a string produced according to the formatting string
* <i>format</i>.
*/
function sprintf(string $format, $args = null, $_ = null): string {}
For 99% of use cases I feel the above is more than adequate for understanding how the function is working and most importantly, how to use it.
The other and more foolproof way is to checkout the PHP Git. Here is the actual source for sprintf
:
#include <stdio.h>
#include <stdarg.h>
#include "php.h"
#ifdef PHP_WIN32
#include "config.w32.h"
#else
#include <php_config.h>
#endif
PHPAPI int
php_sprintf (char*s, const char* format, ...)
{
va_list args;
int ret;
va_start (args, format);
s[0] = '\0';
ret = vsprintf (s, format, args);
va_end (args);
return (ret < 0) ? -1 : ret;
}
Source: https://github.com/php/php-src/blob/master/main/php_sprintf.c
Upvotes: 2