我对编程还比较陌生,在阅读语言文档时仍然对自己缺乏信心。 php.net 对 PHP echo 的描述是:
void echo ( string $arg1 [, string $... ] )
因此在我看来,输入参数必须是字符串类型。然而,echo 可以处理数字。例如,代码...
<p><?php echo 3; ?></p>
...成功地将 3 打印到页面上。那么 $arg1 和 $... 的参数类型不应该是伪类型 mixed
(而不仅仅是字符串)来表明 echo 将接受字符串或数字?否则,我如何从文档中推断数字参数是可以接受的?
I'm relatively new to programming and am still unsure of myself when reading language documentation. The description of PHP's echo from php.net is:
void echo ( string $arg1 [, string $... ] )
Thus it seems to me that the input parameter (s) must be of type string. However, echo works with numbers. For example, the code...
<p><?php echo 3; ?></p>
...successfully prints 3 to the page. Then shouldn't the parameter type for $arg1 and $... be the pseudo-type mixed
(instead of just string) to show that echo will accept strings or numbers? Otherwise, how would I be able to infer from the documentation that number parameters are acceptable?
发布评论
评论(6)
echo
将 将其参数转换为字符串。echo
will cast its arguments to a string.实际上在手册中,
string
也是一种伪类型。不是像mixed
这样的真正的伪类型(请参阅 Pseudo - PHP 文档中使用的类型和变量)。更准确地说,
string
是松散的输入:由于这适用于变量,因此同样适用于其他类型的表达式:表达式的类型由使用该表达式的上下文决定。
在您的例子中,表达式
3
用于echo
函数的字符串上下文中。 PHP 使用3
作为字符串没有任何问题,因此您不会收到错误并且它会显示(作为字符串)。echo
需要string
参数。当您传递字符串、数字、布尔值、NULL 或资源的(变量)表达式时(请参阅 类型),所有这些类型都用作字符串。因此,每当您看到string
作为类型时,只需使用字符串表达式即可。这并不意味着您需要显式地将表达式定义为字符串才能使其工作,因为 PHP 没有显式类型定义。Actually in the manual,
string
is sort of a pseudo-type as well. Not a real pseudo-type likemixed
is (see Pseudo-types and variables used in the PHP documentation).To be more precise,
string
is loosely typed:As this is for variables, the same applies to other types of expressions: an expression's type is determined by the context in which the expression is used.
In your case, the expression
3
is used in the string context of theecho
function. PHP has no problem at all to use3
as string, so you don't get an error and it's displayed (as string).echo
expectsstring
parameters. When you pass a (variable) expression that is a string, a number, a boolean, NULL or a Resource (see Types), all these types are used as strings. So whenever you seestring
as the type, just use a string expression. It does not mean that you need to explicitly define an expression to be string to make it work, as PHP does not have explicit type definition.PHP 处理 var 的方式相当松散,请在此处阅读更多内容:
http://php.net /manual/en/language.types.type-juggling.php
Php handles var rather loosely, read more here:
http://php.net/manual/en/language.types.type-juggling.php
php 可以自动改变类型(正确或错误取决于你站在哪个角落),所以在大多数情况下它不关心......
php can automagic change type (rightly or wrongly depending on which corner you stand) so it cares little in most cases...
AFAIK php 确实将变量(尤其是混合变量)转换为字符串,
例如。
如果您回显 $myArray ,它将导致
AFAIK php does cast variables (especially mixed vars) into string
like for example..
if you will echo out $myArray it will result into
这并不是说
echo
改变了任何东西的类型。echo()
是一种语言构造,其参数将被视为字符串。因此,它期望传递给它的每个参数都有一个刺痛。然而,语言级别的 PHP 在处理数据类型方面没有任何问题,因为它是动态的 松散类型语言(如 Lisp 或 Perl,与 Python 或 C 的严格类型相比)打字)。
因此,即使(几乎)1 你所说的一切基本上都是一个表达式,这些句子中的每一个都有一个返回值。
每个值都有一个内在类型。例如,假设
4
是integer
类型的值。这永远是真的。因此,如果您测试类型完整性,
isint(4)
将始终返回值true
(类型为boolean
)。然而,PHP 中的方法是,应该由语言关心在运行时提升为所需类型,而不是程序员必须考虑变量的类型,这些类型在某些用例中甚至可能是可变的(例如通用快速排序函数构造)。
因此,每当您将一个表达式用作另一个表达式的一部分时,都会有一个类型推断过程,通过该过程,每个子表达式都会被提升为推断类型(通过已解析的超级表达式的部分)。
假设我们有表达式
"13" 。 37.
。在 PHP 中,.
是字符串连接运算符。"13"
是一个字符串
类型的表达式。那么,.
的右运算符应该是一个字符串。但事实并非如此,Zend 引擎寻找一种将37
整数表达式提升为字符串的简单方法。它找到一个:字符串"37"
,由类似 Cprintf("%d", value)
的表达式生成。任何数据类型都会发生相同的推理过程。在高级编程主题中,这可能会有些令人不安,因为人们不能总是只检查表达式的值,还要检查它的类型,因为类型的自动提升可能会触发天真的程序员可能拥有的真值通过不处理表达式类型而不受检查。
在如下环境中尤其如此:
如果您正在查找字节的 ASCII 表示形式,请使用 chr()
1 有一些例外,例如
namespace
构造、declare
构造,这不是 Lisp :-DIt's not that
echo
changes the type of anything.echo()
is a language construct whose parameters will be treated as strings. So, it expects a sting as each parameter passed to it.PHP at the Language Level, however, doesn't have any problem mangling with the data types, as it is a dynamic loosely typed language (as Lisp or Perl, and as compared to Python or C's strict typing).
So, even if (almost)1 everything you say is basically an expression, each of these sentences have a return value.
Every value has an intrinsic type. Let's say, for example, that
4
is a value of theinteger
type. This will always be true.So, if you test for type integrity,
isint(4)
will always return the valuetrue
(of typeboolean
).However, the approach in PHP is that it should be the language who cared about promoting to the needed types in runtime, not about the programmer having to think about the types of the variables, which might even be mutable in some use cases (like a generic quick sort function construction).
So whenever you use an expression as part of another, there is a process of type inference by which every sub-expression is promoted to the inferred type (by the part of the super-expression which has been already parsed).
So let's say we have the expression
"13" . 37
. In PHP, the.
is the string concatenation operator."13"
is astring
-type expression. So then, the right operator of the.
is expected to be a string. As it is not, the Zend Engine looks for an easy way of promoting the37
integer expression to string. It finds one: the string"37"
, generated by a Cprintf("%d", value)
-like expression.The same inference process happens with any data type. This can be somewhat disturbing in advanced programming topics, as one must not always just check about the value of an expression, but also the type of it, as the auto-promotion of types may be triggering a truth value that a naïve programmer could have left unchecked by not dealing with expression types.
This is particularly true in environments such as the following:
If you are looking for the ASCII representation of a byte, use chr()
1 There are exceptions, like the
namespace
construct, thedeclare
construct, this is not Lisp :-D