$gen_previous = "Previous";
$gen_next = "Next";
$gen_jumptopage = "Jump to page %1\$s of %2\$d";
$gen_resultinfo = "Viewing %1\$d-%2\$d of %3\$d messages";
$gen_loading = "Loading...";define( "PVM_MSGSTORE_ASF_STRING", "Showing %1$d-%2%d of %3d" );以上是一个PHP脚本文件,我在里面发现一些奇怪的参数写法。http://php.net/manual/en/function.sprintf.php
根据这个文档里的例子3:Example #3 Argument swapping
<?php
$format = 'The %2$s contains %1$d monkeys'; //PHP文档里的写法
printf($format, $num, $location);
?><?php
$format = 'The %2\$s contains %1\$d monkeys';   //嵌入斜杠
printf($format, $num, $location);
?>我的问题是如果在参数中加一个斜杠,对输出的结果有影响吗?这种加斜杠参数的写法合法吗?
我手头没有PHP程序,有哪位高手能实际跑一下这个printf语句,告诉我结果吗?谢谢。

解决方案 »

  1.   

    斜杠是转义字符,\$s表示输出\$s字符,而不是将$s对应的数值打印出来
      

  2.   


    <?phpecho $gen_previous = "Previous";
    echo "<br/>";
    echo $gen_next = "Next";
    echo "<br/>";
    echo $gen_jumptopage = "Jump to page %1\$s of %2\$d";
    echo "<br/>";
    echo $gen_resultinfo = "Viewing %1\$d-%2\$d of %3\$d messages";
    echo "<br/>";
    echo $gen_loading = "Loading...";
    echo "<br/>";define( "PVM_MSGSTORE_ASF_STRING", "Showing %1$d-%2%d of %3d" );
    echo PVM_MSGSTORE_ASF_STRING;
    echo "<br/>";$format = 'The %2$s contains %1$d monkeys'; //PHP文档里的写法
    printf($format, $num, $location);
    echo "<Br/>";
    $format = 'The %2\$s contains %1\$d monkeys'; //嵌入斜杠
    printf($format, $num, $location);
    /*
    Previous
    Next
    Jump to page %1$s of %2$d
    Viewing %1$d-%2$d of %3$d messages
    Loading...
    Showing %1-%2%d of %3d
    The contains 0 monkeys
    The $s contains $d monkeys
     */
    ?>
      

  3.   

    那按二楼的意思 这句用作printf 输出就没什么意义了?
    $gen_jumptopage = "Jump to page %1\$s of %2\$d";$format = 'Jump to page %1\$s of %2\$d'; //嵌入斜杠
    printf($page, $pages)结果就是 “Jump to page %1\$s of %2\$d” ???
      

  4.   

    $num=3;
    $location='tree';$format='The %2$s contains %1$d monkey'."\n";
    printf($format,$num,$location);$format="The %2$s contains %1$d monkey\n";
    printf($format,$num,$location);$format="The %2\$s contains %1\$d monkey\n";
    printf($format,$num,$location);
    注意单双引号!
    上面的结果为:
    The tree contains 3 monkey
    The  contains  monkey
    The tree contains 3 monkey