简体   繁体   English

PHP:什么是语言结构,我们为什么需要它们?

[英]PHP: What are language constructs and why do we need them?

I keep coming across statements like:我不断遇到这样的陈述:

  • "echo is a language construct but print is a function and hence has a return value" “echo 是一种语言结构,而 print 是一个函数,因此有一个返回值”
  • "die is a language construct" “死是一种语言结构”

My question is what are these language constructs and more importantly why do we need them?我的问题是这些语言结构是什么,更重要的是我们为什么需要它们?

Language constructs are hard coded into the PHP language.语言结构被硬编码到 PHP 语言中。 They do not play by normal rules.他们不按正常规则玩。

For example, whenever you try to access a variable that doesn't exist, you'd get an error.例如,每当您尝试访问一个不存在的变量时,您都会收到错误消息。 To test whether a variable exists before you access it, you need to consult isset or empty :要在访问变量之前测试它是否存在,您需要咨询issetempty

if (isset($foo))

If isset was a normal function, you'd get a warning there as well, since you're accessing $foo to pass it into the function isset .如果isset是一个普通函数,您也会在那里收到警告,因为您正在访问$foo以将其传递给函数isset Since isset is a language construct though, this works without throwing a warning.不过,由于isset是一种语言结构,因此可以在不发出警告的情况下工作。 That's why the documentation makes a clear distinction between normal functions and language constructs.这就是文档明确区分普通函数和语言结构的原因。

Language constructs are what makes up the language: things like "if" "for" "while" "function" and so on.语言结构构成了语言:诸如“if”“for”“while”“function”之类的东西。

The mentions in the PHP manual of things like "echo", "die" or "return" are there to make it clear that these are NOT functions and that they do not always behave like functions. PHP 手册中提到的诸如“echo”、“die”或“return”之类的东西是为了清楚地表明它们不是函数,并且它们的行为并不总是像函数一样。

You could call "echo" as "echo()" so it may confuse beginners.您可以将“echo”称为“echo()”,这样可能会使初学者感到困惑。 That's why they put the clear disinction in the manual.这就是为什么他们在手册中明确区分的原因。 To make it absolutely clear to everyone.为了让大家一目了然。

Other examples for language constructs that could be mistaken for functions are "array()", "list()" and "each()".其他可能被误认为函数的语言结构示例是“array()”、“list()”和“each()”。

Not all of a language can be functions.并非所有语言都可以是函数。 There must be some base, somewhere, on which you implement those first functions.必须有一些基础,在某个地方,您可以在其上实现那些第一个功能。 The elements of this base are the language constructs (alternately, built-ins ).这个基础的元素是语言结构(或者,内置的)。 They don't always behave like "normal" functions do.它们的行为并不总是像“正常”函数那样。

To understand the answer for this question you must understand how parsers work.要了解这个问题的答案,您必须了解解析器的工作原理。 A language is defined by syntax and the syntax is defined through keywords.语言是由语法定义的,而语法是通过关键字定义的。

The language constructs are pieces of code that make the base of PHP language.语言结构是构成 PHP 语言基础的代码片段。 The parser deals with them directly instead of functions.解析器直接处理它们而不是函数。

Some things are just not possible using normal functions, consider this snippet:有些事情使用普通函数是不可能的,请考虑以下代码段:

list($el1, $el2) = array('el1', 'el2');

What it does is it takes the elements from a non-associative array and assigns the values to the variables defined in the list() construct.它的作用是从非关联数组中获取元素并将值分配给list()构造中定义的变量。

Simply cannot be done with functions :)根本无法用函数来完成:)

A more subtle example is isset and empty .一个更微妙的例子是issetempty Though they look like functions, they one thing that's not possible with functions alone – they do not generate "variable is undefined" or "undefined index" notices.尽管它们看起来像函数,但它们是单独使用函数无法实现的一件事——它们不会生成“变量未定义”或“未定义索引”的通知。

language constructs can be formed in more than one way and has a return-value语言结构可以以多种方式形成并具有返回值

print("asdf"); is as possible as print "asdf";尽可能print "asdf"; and will return 1.并将返回 1。

echo("asdf"); is equal to echo "asdf;"等于echo "asdf;" but has no return-value.但没有返回值。

die("asdf"); is equal to exit("asdf");等于exit("asdf"); and hasn't a return-value too.并且也没有返回值。

For the sake of completeness, a language construct is any instruction which is built into the language itself, while a function is an additional block of code.为了完整起见,语言结构是任何内置于语言本身的指令,而函数是附加的代码块。

In some cases, a language may choose to build in a particular feature or to rely on a separate function.在某些情况下,语言可能会选择构建特定功能或依赖单独的功能。

For example, PHP has the print language construct, which outputs a string.例如,PHP 具有print语言结构,它输出一个字符串。 Many other languages, such as C don't build it in, but implement it as a function.许多其他语言,例如C没有内置它,而是将它实现为一个函数。 There might be technical reasons for taking one or other approach, but sometimes it is more philosophical — whether the feature should be regarded as core or additional.采取一种或其他方法可能有技术原因,但有时更具哲学性——该功能应该被视为核心还是附加。

For practical purposes, while functions follow a rigid set of logistic rules, language constructs don't.出于实际目的,虽然函数遵循一组严格的逻辑规则,但语言结构却没有。 Sometimes, that's because they may be doing something which would otherwise traumatise a regular function.有时,那是因为他们可能正在做一些会对正常功能造成创伤的事情。 For example, isset(…) , by its very purpose, may be referencing something which doesn't exist.例如, isset(…) ,就其目的而言,可能会引用不存在的东西。 Functions don't handle that at all well.函数根本不能很好地处理这个问题。

Here are some of the characteristics of language constructs:以下是语言结构的一些特征:

  • Many don't require parentheses;许多不需要括号; some do sometimes .有些人有时会
  • Language Constructs are processed in a different stage;语言结构在不同的阶段进行处理; functions are processed later函数稍后处理
  • Some Language Constructs, such as isset do things which would be impossible as functions;一些语言结构,比如isset做一些作为函数不可能的事情; some others, such as Array(…) could have gone either way.其他一些,例如Array(…)可能会采用任何一种方式。
  • Some Language Constructs certainly don't look like functions.一些语言结构当然看起来不像函数。 For example, the Array(…) construct can be written as […] .例如, Array(…)结构可以写成[…]
  • As the documentation keeps reminding us, language constructs cannot be referenced as variable variables.正如文档不断提醒我们的那样,语言结构不能作为变量引用。 So $a='print_r'; $a(…);所以$a='print_r'; $a(…); $a='print_r'; $a(…); is OK, but $a='print'; $a(…);没问题,但是$a='print'; $a(…); $a='print'; $a(…);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM