View Full Version : switch case - Parse error: syntax error

03-10-2009, 02:22 AM
Hi I am using this switch case learnt from Davidj:

$x = $row_checkRS['contact_how'];
switch($x) {
case "Email":
echo "<a href=\"contactbusiness.php?listingID=<?php echo $row_checkRS['listingID']; ?>\"> - Email Us</a>";
case "Mail CV":
echo "Mail Your CV To Us";
case "Telephone":
echo "Contact Us Via Telephone";

It gives this error and I know it is to do with the link to detail:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
What does this mean?

Thanks 4 your help..

03-10-2009, 03:15 AM
hard code a value instead of $x and see if you have the same result

03-10-2009, 04:28 AM
Sorry not sure what you mean - it is what I am using the switch case for. I know it is something to do with the go to detail link and mix up of single and double quotes but the $x = $row_checkRS['contact_how']; is what is coming from the database.
I have tried to do an if else but still can't get it to work.
It is a field from the job listing table e.g. so it is a foreign key.
In the contact_how table is the values: Email, Mail CV and Telephone.
If Email is showing from the database I want to allow users to apply via email and that is why I am using a url link using the listingID.

Any suggestions?


03-10-2009, 04:35 AM
yes i just meant as a step in debugging, ( or echo the value to be sure it is being returned)

03-10-2009, 04:42 AM
Yes it is returned I echo it out above the statement to check.

03-10-2009, 04:57 AM
Hi I fixed it:
Got rid of switch case and used this:
<a href="contactbusiness.php?listingID=<?php echo $row_checkRS['listingID']; ?>">
<?php if (!empty($row_checkRS['contact_how']) && $row_checkRS['contact_how'] == 'Email') echo 'Email Us'; ?>

Works a treat!

03-10-2009, 09:31 AM
it better to assign any output to a variable especially within a switch

$x = $row_checkRS['contact_how'];

switch($x) {

case "Email": $link = "<a href=\"contactbusiness.php?listingID=".$row_checkRS['listingID']." \"> - Email Us</a>"; break;

case "Mail CV": $link = "Mail Your CV To Us"; break;

case "Telephone": $link = "Contact Us Via Telephone"; break;


then just echo $link where you want the output to display

possible reason for yours not working is that you used <?php tags inside php

notice how i used the concatination operator [.] to add the PHP $row_checkRS['listingID'] array value

04-07-2009, 06:49 AM
Thanks will try it
Also found out I should have done this:

echo '<a href="contactbusiness.php?listingID=' .
$row_checkRS['listingID'] .'"> - Email Us</a>';

You cannot nest PHP tags inside a PHP code block.
You cannot use quotes around an array element in a double-quoted string.

Learning many ways...thanks