dounuogong0358 2017-07-17 08:04
浏览 91

php sprintf函数的php源代码

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.

  • 写回答

2条回答 默认 最新

  • douyan2680 2017-07-17 08:45
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。