字符串中的变量替换

PHP 4 添加了一个新的机制以替换字符串中的变量。现在可以访问字符串中的对象成员变量和多维数组元素。

如果想这么做,需要使用大括号将一个美元符号和变量名括起来,就像这样:{$...}

要在字符串中嵌入对象成员变量,只要简单的写 "text {$obj->member} text"。而在 PHP 3 中,要使用 "text ".$obj->member." text"

这个改进可以使代码更易读,但这可能会破坏用 PHP 3 写的代码。但是很容易修正它:在代码中查找字符串中的 {$,使用搜索替换工具将它替换为 \{$


add a note add a note User Contributed Notes
K.Tomono
15-Apr-2003 09:54
>But you can easily check for this kind of problem by
> checking for the character combination {$ in your code
> and by replacing it with \{$ with your favorite
> search-and-replace tool.

A code which was written below may exist.

  if("aaa"){$foo="bar";}

I've done like the following for the `easy checking`.  (on Linux - tcsh)

  egrep '".*\{\$.+\}.*"' *.php

also using Regexp Search of a Windows Editor (Emacs compatible.)

Search Word  : ".*{\$.+}.*"

I'm not so good at treating Regexp. so there may be a lack of
code pattern.