文字列を作成するための事前定義されたパターンがあります。これは、スクリプト全体で定期的に作成する必要があります。
$str="$first - $second @@ $third"; // pattern for building the string
$first="word1";
$second="word2";
$third="word3";
$string= ..?? // string should be built here based on the pattern
現在、私はeval
最初に定義されたパターンに基づいて文字列を適切に生成するために使用しています。しかし、これは時々起こり、eval
一般的に悪いので、私は別の方法を見つけたいと思います。
パターンはすべてのコードの上で1回だけ定義され、すべてのスクリプトのパターンを1行で編集できることに注意してください。したがって、何が$string
変化するかについては触れないでください。
試しcreate_function
ましたが、同じ数の引数が必要です。を使用eval
すると、パターンを簡単に変更できますが、create-function
を使用すると、スクリプト全体を変更する必要があります。たとえば、文字列パターンをに変更する場合
$str="$first @@ $second"; // One arg/var is dropped
evalの例:
$str="$first - $second @@ $third"; // Pattern is defined one-time before codes
$first="word1";
$second="word2";
$third="word3";
eval("\$string = \"$str\";");
create_function例:
$str=create_function('$first,$second,$third', 'return "$first - $second @@ $third";');
$string=$str($first,$second,$third);