現在我有一個關於如何查看sprintf
php funciton的源代碼的問題。因爲我想知道函數在哪裏以及如何執行。 我可以通過grep -r 'PHP_FUNCTION(round)'
找到一些函數,如math.c中的圓函數,但是sprintf與循環函數不相似。php sprintf函數的php源代碼
回答
有兩種很好的方法來查看PHP源代碼。
首先最簡單的就是使用你的IDE。例如在Netbeans ctrl+right click
上的任何函數都會帶你到它的源頭。這種方法毫無疑問是最快的,編寫代碼時,它是瞭解函數如何工作的最簡單方法。
但是,您不一定會獲得完整的源代碼。以sprintf
爲例,您只需獲得以下內容。
/**
* (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 {}
有關使用案例99%,我覺得上面的是綽綽有餘瞭解函數是如何工作的,最重要的是,如何使用它。
另一種更簡單的方法是結賬PHP Git。下面是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;
}
來源:https://github.com/php/php-src/blob/master/main/php_sprintf.c
您發佈的源代碼不是PHP ['sprintf()'](http://php.net/manual/en/function.sprintf.php)函數的代碼。在PHP中沒有'char *'這樣的東西,PHP版本的'sprintf()'返回一個字符串,而不是一個數字。當標準C庫不遵守C語言的C99標準時,這是PHP內部使用的'sprintf()'的替代實現,而不是標準C庫提供的。 – axiac
是不是所有的PHP核心都是C? – Doug
PHP的確是用C編寫的。解釋器和PHP函數。我的觀點是,當PHP腳本執行'sprintf(「...」)'時,您發佈的代碼不會運行。這段代碼在解釋器中運行,它也是許多PHP函數('password_hash()','addcslashes()'等)的實現的一部分,但不是作爲* printf()PHP函數的一部分。 – axiac
PHP實現使用php_formatted_print()
內部函數,它處理所有*printf()
PHP函數(printf()
,sprintf()
,fprintf()
,vprintf()
,vsprintf()
, vfprintf()
)。他們都做類似的處理,只有輸出的目的地不同。
C函數user_sprintf()
(名爲這種方式,以避免與由標準C庫提供的sprintf()
功能相沖突)被聲明爲sprintf()
PHP function的the implementation。
Its code很簡單(硬質的提升是通過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);
}
/* }}} */
有實現其他*printf()
PHP功能類似的功能。他們處理由php_formatted_print()
返回的值的方式不同。
公平競賽。我認爲**避免**閱讀php_formatted_print()來源可能符合每個人的利益,除非您想要腦部疼痛。 'php_sprintf'和'zend_sprintf'服務的目的是什麼? – Doug
另外,如果你看看main/php.h,你會在第41行看到#undef sprintf #define sprintf php_sprintf /ext/standard/basic_functions.c中的源是否覆蓋main/php.h中定義的內容? https://github.com/php/php-src/blob/6035ebd4a6c2c81eaf7b2ac71f6e23cb04e2c000/main/php.h#L41 – Doug
不可以。「main」目錄包含解釋器的代碼。 'ext'目錄包含PHP擴展的實現; 'ext/standard'包含[核心擴展名](http://php.net/manual/en/extensions.membership.php#extensions.membership.core)的代碼(除了在其中託管的'SPL'外自己的目錄)。 'ext/standard/basic_functions.c'包含一個結構體列表('const zend_function_entry basic_functions []'),它告訴解釋器C函數('user_sprintf')實現一個PHP函數('sprintf')以及它的參數列表如何顯示像('arginfo_sprintf')。 – axiac
- 1. PHP,Printf,Sprintf函數
- 2. 內置函數的PHP源代碼
- 3. 在源代碼中查找PHP函數?
- 4. 如何用sprintf讀取php代碼
- 5. PHP類源代碼
- 6. PHP函數代碼錯誤
- 7. PHP PDO函數中斷PHP代碼
- 8. PHP函數來縮進PHP代碼?
- 9. PHP中的開源代碼
- 10. PHP的PDO源代碼
- 11. php源代碼中的similar_text
- 12. Sass源代碼中的PHP代碼
- 13. 內置php函數的源代碼位於何處?
- 14. PHP代碼顯示在源代碼
- 15. PHP複雜Sprintf
- 16. PHP sprintf格式
- 17. sprintf和php
- 18. PHP輸出源代碼
- 19. php ziparchive類源代碼
- 20. PHP file_get_contents源代碼位置
- 21. 用PHP閱讀源代碼
- 22. 轉換ASP.net源代碼PHP
- 23. PHP(XML源代碼)到HTML
- 24. 更改源代碼php
- 25. 如何在PHP源代碼
- 26. 保護PHP源代碼
- 27. MySQL源代碼通過PHP
- 28. 加密PHP源代碼
- 29. AJAX獲取PHP源代碼
- 30. PHP的sprintf逃逸%
https://github.com/php/php-src/blob/c8aa6f3a9a3d2c114d0c5e0c9fdd0a465dbb54a5/ext/standard/formatted_print.c#L662 –
非常感謝這個簡單的問題,我應該用grep「sprintf的」。 – GenialX
'grep PHP_FUNCTION(user_sprintf)'猜測前綴是因爲C已經有一個名字相同的函數 – apokryfos