%S在PHP,HTML或XML中的含义是什么? [英] What does %S mean in PHP, HTML or XML?

查看:148
本文介绍了%S在PHP,HTML或XML中的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 Webmonkey的PHP和MySql教程,第2课。我认为这是一个PHP文字。 %s 是什么意思?它至少在第一对代码块中的while循环中位于 print_f()函数中。

printf(< tr>< td>%s%s< / td>< td>%s< / td>< / tr> n,...

示例:

  $ str1 ='best'; 
$ str2 ='world';

$ say = sprintf('Tivie是%s!中的%s!',$ str1 ,$ str2);
echo $ say;

这会输出:


Tivie是世界上最好的!

:有更多的占位符(%s表示字符串,%d表示十进制数字,等等......)



订单:



传递参数的顺序数。如果你用$ str2转换$ str1作为

  $ say = sprintf('Tivie是%s中的%s!' ,$ str2,$ str1); 

它会打印

$ b $


然而,你可以改变参数的阅读顺序,像这样:

  $ say = sprintf('Tivie是%1 $ s!中的%2 $ s',$ str2, $ STR1); 

这将正确地打印句子。




另外,请记住,PHP是一种动态语言,不需要(或支持)明确的类型定义。这意味着它根据需要处理变量类型。在sprint中,它意味着如果你传递一个字符串作为数字占位符(%d)的参数,那么该字符串将被转换为数字(int,float ...),这可能会产生奇怪的结果。以下是一个例子:

  $ onevar = 2; 
$ anothervar ='pocket';
$ say = sprintf('我有%d巧克力在我的%d。',$ onevar,$ anothervar);
echo $ say;

这会打印


我的巧克力有2个。


更多阅读 PHPdocs


I'm looking at Webmonkey's PHP and MySql Tutorial, Lesson 2. I think it's a php literal. What does %s mean? It's inside the print_f() function in the while loops in at least the first couple of code blocks.

printf("<tr><td>%s %s</td><td>%s</td></tr>n", ...

解决方案

with printf or sprintf characters preceded by the % sign are placeholders (or tokens). They will be replaced by a variable passed as an argument.

Example:

$str1 = 'best';
$str2 = 'world';

$say = sprintf('Tivie is the %s in the %s!', $str1, $str2);
echo $say;

This will output:

Tivie is the best in the world!

Note: There are more placeholders (%s for string, %d for dec number, etc...)


Order:

The order in which you pass the arguments counts. If you switch $str1 with $str2 as

$say = sprintf('Tivie is the %s in the %s!', $str2, $str1); 

it will print

"Tivie is the world in the best!"

You can, however, change the reading order of arguments like this:

$say = sprintf('Tivie is the %2$s in the %1$s!', $str2, $str1);

which will print the sentence correctly.


Also, keep in mind that PHP is a dynamic language and does not require (or support) explicit type definition. That means it juggles variable types as needed. In sprint it means that if you pass a "string" as argument for a number placeholder (%d), that string will be converted to a number (int, float...) which can have strange results. Here's an example:

$onevar = 2;
$anothervar = 'pocket';
$say = sprintf('I have %d chocolate(s) in my %d.', $onevar, $anothervar);
echo $say;

this will print

I have 2 chocolate(s) in my 0.

More reading at PHPdocs

这篇关于%S在PHP,HTML或XML中的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆