what is the problem of T_VARIABLE ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lancet2003
Forum Newbie
Posts: 20
Joined: Sat Aug 09, 2003 11:29 pm

what is the problem of T_VARIABLE ?

Post by lancet2003 »

when I run the following code, there also shows
Parse error: parse error, unexpected T_VARIABLE in c:\inetpub\wwwroot\hidden.php on line 5


<HTML>
<HEAD></HEAD>
<BODY>
<? php
$Message1="Bugs Bunny"; THERE IS A PROBLEM HERE
$Message2="Homer Simson";
$Message3="Ren & Stimpy";
echo "<FORM method="get" action="text.php">;
echo "what price of car are you looking for":
echo "<SELECT name="Price";
echo "<OPTION> $Message1</OPTION>";
echo "<OPTION> $Message2</OPTION>";
echo "<OPTION>$Message3</OPTION>";
echo "</SELECT><br>";
echo"<input type=hidden name=hidden value='$Message1'>";
echo"<input type=hidden name=hidden value='$Message2'>";
echo"<input type=hidden name=hidden value='$Message3'>";
echo"<input type=submit>";
echo"</Form>"
?>
</BODY>
</HTML>
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

I recommend you read this tutorial on HTML: http://www.w3schools.com/html/default.asp

Then read memorize this:
http://www.php.net/manual/en/language.b ... ax.phpmode

Here is code that should work for you:

Code: Select all

<? php 
$Message1 = "Bugs Bunny";
$Message2 = "Homer Simson"; 
$Message3 = "Ren & Stimpy"; 
echo '<FORM method="get" action="text.php">'; 
echo "what price of car are you looking for": 
echo '<SELECT name="Price">'; 
echo "<OPTION> $Message1</OPTION>"; 
echo "<OPTION> $Message2</OPTION>"; 
echo "<OPTION>$Message3</OPTION>"; 
echo "</SELECT><br>"; 
echo '<input type="hidden" name="hidden1" value="$Message1">'; 
echo '<input type="hidden" name="hidden2" value="$Message2">'; 
echo '<input type="hidden" name="hidden3" value="$Message3">'; 
echo '<input type="submit">'; 
echo "</Form>" 
?>
To be even more efficient when outputting HTML I suggest you use echo's "here document" syntax:
http://us4.php.net/echo
lancet2003
Forum Newbie
Posts: 20
Joined: Sat Aug 09, 2003 11:29 pm

Thanks, " ' " instead of " " "

Post by lancet2003 »

Hi:
When I embed the HTML code in PHP, I should use " ' " instead of " " ",
is that right?

Thanks
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Discussed just earlier in another thread...
http://www.devnetwork.net/forums/viewtopic.php?t=11728

Take a look at that, and check out McGruff's link at the bottom...
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Thats what he is saying :o)

You could also escape the double-quotes and do it your way

e.g

Code: Select all

echo "<input type="hidden" name="hidden1" value="$Message1">";
Mark
lancet2003
Forum Newbie
Posts: 20
Joined: Sat Aug 09, 2003 11:29 pm

Why value of variables Message1.2.3 can not be transfered?

Post by lancet2003 »

Here is the hidden.php code, there is no problem in it now.

Code: Select all

<?php
<HTML> 
<HEAD></HEAD> 
<BODY> 
<?php

$Message1 = "Bugs Bunny"; 
$Message2 = "Homer Simson"; 
$Message3 = "Ren & Stimpy"; 
echo '<FORM method="get" action="text.php">'; 
echo "what price of car are you looking for"; 
echo '<SELECT name="Price">'; 
echo "<OPTION> $Message1</OPTION>"; 
echo "<OPTION> $Message2</OPTION>"; 
echo "<OPTION>$Message3</OPTION>"; 
echo '</SELECT><br>'; 
echo '<input type="hidden" name="hidden1" value=$Message1>'; 
echo '<input type="hidden" name="hidden2" value="$Message2">'; 
echo '<input type="hidden" name="hidden3" value="$Message3">'; 
echo '<input type="submit">'; 
echo '</Form>';

?>
</BODY> 
</HTML>
?>
and here is text.php code

Code: Select all

<?php
<HTML> 
<HEAD></HEAD> 
<BODY> 

<?php 
echo "the three optons were <br>";
echo "$hidden1<BR>";
echo "$hidden2<BR>";
echo "$hidden3<BR>";
echo "<BR> You selected:<BR>";
echo "$Price"
?> 
</BODY> 
</HTML> 
?>
when I run, the outcome always shows like this
< the three optons were
$Message1
$Message2
$Message3

You selected:
Homer Simson

why the value of varible message1, message2, message3 can not be transfer?
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Replace the three <input type=hidden lines with:

Code: Select all

echo "<input type="hidden" name="hidden1" value="".$Message1."">";
echo "<input type="hidden" name="hidden2" value="".$Message2."">";
echo "<input type="hidden" name="hidden3" value="".$Message3."">";
Or

Code: Select all

echo '<input type="hidden" name="hidden1" value='.$Message1.'>';
echo '<input type="hidden" name="hidden2" value='.$Message2.'>';
echo '<input type="hidden" name="hidden3" value='.$Message3.'>';
Or

Code: Select all

echo "<input type='hidden' name='hidden1' value='$Message1'>";
echo "<input type='hidden' name='hidden2' value='$Message2'>";
echo "<input type='hidden' name='hidden3' value='$Message3'>";
lancet2003
Forum Newbie
Posts: 20
Joined: Sat Aug 09, 2003 11:29 pm

Thanks

Post by lancet2003 »

Thanks, I will see the related recommended turoial to understand these 3 solutions.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Lancet, you really shouldn't have "<head></head>" you should have the page title inside the head tags and such.
lancet2003
Forum Newbie
Posts: 20
Joined: Sat Aug 09, 2003 11:29 pm

what is the differences of "" , '' and \"

Post by lancet2003 »

Thanks all.
and I run the qartis's code, everyghing is OK, BUt still confused.
Can someone tell me what is the differences of "" , '' and \" \"
or tell me where can I get the tutoril for this. I guess there is some understanding problem of embedding HTML in PHP, right?



See the following codes:
echo '<SELECT name="Price">';
echo "<OPTION> $Message1</OPTION>";
echo "<OPTION> $Message2</OPTION>";
echo "<OPTION>$Message3</OPTION>";

Code: Select all

echo "</SELECT><br>";
echo '<SELECT name="Price">';
echo "<OPTION> $Message1</OPTION>";
echo "<OPTION> $Message2</OPTION>";
echo "<OPTION>$Message3</OPTION>";

Code: Select all

echo '</SELECT><br>'
;

THe only differences is "" and '', but they all run Ok.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

the differences are in how the php server reads it

if you have a string inside " " then the php server will resolve all variables inside it
if you have it in ' ' then it wont, it will just print the variable name
\" and '' are used to show said characters inside those strings
eg:

Code: Select all

$foo = 'variable';
echo "this is a $foo";
echo 'this is a $foo';
echo 'this is a ''$foo''';
echo this is a '$foo'';

//the best solution, to prevent confusion in strings, is to use '.' which basically adds strings together
//ie.
echo 'this is a ' . $foo;

//results:
this is a variable
this is a $foo
this is a '$foo'
parse error
this is a variable
i usually use the last one, because its much easier to read.
Also note that if you are using ' ' you dont have to use \" (just use ") and if you are using " " you dont need to use ''
Post Reply