2012-04-07 20 views
1

データベースクエリから返された行数に基づいてフォームに値をエコーし​​ようとしています。エラーが発生する構文解析エラー:構文エラー、予期しないT_ECHO、 '、'または ';'エコー変数(html形式)

おそらく私はこれにかなり新しいと言うことができます。誰も私が変数をエコーするのを助けることができる?私は$ num_rowsがvar_dumpショーを使うときに値を返すことを知っています。おかげ

<? 

if($num_rows <= 10) { 

echo '</br></br><form id="h1" class="rounded" action="4.php" target="" 
method="post"/> 
<input type="submit" name="submit" class="button" value="10" /><br> 
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/> 
</form>'; 
} 
if($num_rows >10) { 
echo '</br></br><form id="h2" class="rounded" action="4.php"  
target="_blank" method="post"/> 
<input type="submit" name="submit" class="button" value="11"/><BR> 
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/> 

</form>'; 
}?> 
+2

メッセージがなくなるまで削除した最後の問題はおそらく問題の原因でした。 –

+0

また、構文解析プログラムは構文エラー(ソースファイル、行番号、およびエラーの説明)を検出した場所を通知します。 – Raffaele

+0

パーサは、echo $ num_rowsである40行目のエラーを識別します。まだ値= "'エコー$ num_rows;'"がエコーされたフォーム内で動作しない理由を理解していない...? – user1022772

答えて

2

両方のコードブロックで、出力を連結するか、2つのステートメントを使用する代わりに、コマンドechoを繰り返します。これを実行しました:

echo '</br></br><form id="h1" class="rounded" action="4.php" target="" 
method="post"/> 
<input type="submit" name="submit" class="button" value="10" /><br> 
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/> 
</form>'; 

これは構文エラーです。代わりに、あなたはこれを行うことができます。

echo '</br></br><form id="h1" class="rounded" action="4.php" target="" 
method="post"/> 
<input type="submit" name="submit" class="button" value="10" /><br> 
<input type="text" name="number_of_tests" value="' . $num_rows . '"/> 
</form>'; 

またはこれを:あなたは理解していない構文エラーを取得するたびに、最初に行うべきことは、ビットを削除(またはコメントアウト)を開始することです

echo '</br></br><form id="h1" class="rounded" action="4.php" target="" 
method="post"/> 
<input type="submit" name="submit" class="button" value="10" /><br> 
<input type="text" name="number_of_tests" value="'; 
echo $num_rows . '"/>'; 
echo '</form>'; 
+0

のようになります。ありがとう。私を怒らせた私が正直なら、それがどういう仕組みか分かりません。 – user1022772

+0

echo some_string。 some_string。 some_string;許可されています。何が最後に別のエコーを追加していないのですか?エコーが1つ必要です。 –

1

これは、あなたが文字列を連結するために使用するコードと出力結果

echo ' some value ' . $variable . ' other text '; 

echo関数は、文字列を出力しているドット(。)演算子を連結文字列中。これは、変数の値を挿入したい場合は、これはこれはあまりにも

$an_array = array('one', 'two', 'three'); 
echo "The first element of the array is {$an_array[0]}" 

参照配列と連携

$a_string = 'I\'m a string'; 
echo "I'm a double quoted string and can contain a variable: $a_string"; 

方法です間違ったコードの種類

echo 'value="'echo $num_rows;'"/>'; 

ですthe PHP manual