解析器行为

解析和执行现在变为两个完全独立的步骤。只有当完全成功的解析后,程序才会执行。

这种改变所带来的一个新的要求是一个脚本文件所包含的另一个文件必须有着完整的语法结构。不能将一个完整的控制结构分散在不同的文件中。这意味着不能在一个文件中开始一个 forwhile 循环、一个 ifswitch 块,而在另一个文件中结束它们,或在另一个文件中使用 elseendifcasebreak

但是在循环或其它控制结构中包含额外的脚本文件是允许的。只要控制的关键词和相应的 {...} 在同一个单元(文件或使用函数 eval() 结合的字符串)中就可以了。

不过,在循环或其它控制结构中包含额外的脚本文件并不是一个好的编程习惯。

另外,一种在 PHP 3 中不常见的代码――从一个 require 的文件中返回值――在 PHP 4 中也不能使用。而从一个 include 文件中返回值还是允许的。


add a note add a note User Contributed Notes
alan at frostick dot de
03-Dec-2001 02:31
Although I reported this in the early days of php4 release as a migration problem, it has yet to appear in the manual. So add this note to clarify things for newcomers to php4 conversions.

An apparent parser change occurred between php3 and php4 to the function unset(). There are now lots of maybe confusing comments about this in the manual under this functions page which you should read.

To clarify an important difference which I spotted, the behaviour of unset() when used within a function on static or global variables gives quite different effects. In php3 the values of such variables were simply reset whereas now the variable is completely removed and any subsequent reference to the same variable name results in a completely new variable being defined - ie. a local function variable is created.

Some misunderstandings about the original php3 function are obvious amongst non-php authors. Quite possibly the present php4 interpretation is what was originally intended in php3, but since it was not implemented in this way can give rise to shocks when the same scripts are run under php4.

e.g.
function foo()
{
  global $bar;
// at this point $bar is global
  $bar="assign this string";
     unset($bar);
// at this point $bar is local
  $bar="overwrite string";
// so this assignment does not affect the global}

foo(); echo $bar;

Under php3 you would get "overwrite string", but under php4 "assign this string".

A similar difference occurs using unset to wipe out an array. The variable is no longer an array, and the memory used by the array is not released for re-use. You are advised to replace any uses of:
 unset($arr);
by instead
 $arr=array();