Syntax error - unexpected “:” [closed]

2019-01-20 20:42发布

问题:

My php script directs to a url depending on which submit button was pressed. However, when I run the test I'm getting an error saying line 4 contains an unexpected ":" but my line 4 is my header script with the url?

I'm confused because I have other scripts similar to this and they don't give me that error. Can anyone tell me what I'm missing, might be simple, I have been caught being simple before.

<?php
if ($_REQUEST['Dish1'] == 'Dish1')
{
header(“Location: http://blahblah”.urlencode($_POST[‘uid’]));
}
else if ($_REQUEST['Dish1'] == 'Dish2')
{
header(“Location: http://blahblah2”.urlencode($_POST[‘uid’]));
}
else if ($_REQUEST['Dish1'] == 'Dish3')
{
header(“Location: http://blahblah3”.urlencode($_POST[‘uid’]));
}
etc.....
?>

回答1:

You are using curly quotes.

Replace all the “ ” and ‘ ’ to " and ' respectively.



回答2:

You are using the wrong quotes... use "" instead of “”. Refer to Wikipedia, you must use typewriter quotes, not curly or inverted commas.

PD: Also PHP Parse error: syntax error, unexpected '.' on line 15 ; )



回答3:

Replace you code with following

<?php

if ($_REQUEST['Dish1'] == 'Dish1')
{
header("Location: http://blahblah.urlencode".($_POST['uid']));
}
else if ($_REQUEST['Dish1'] == 'Dish2')
{
header("Location: http://blahblah2".urlencode($_POST['uid']));
}
else if ($_REQUEST['Dish1'] == 'Dish3')
{
header("Location: http://blahblah3".urlencode($_POST['uid']));
}

?>


回答4:

Is it not much easier to write:

$lookup = array('Dish1' = > 'http://blba1', 'Dish2' = > 'http://blba2');

if( isset($lookup[$_REQUEST['Dish1']]))
  header("Location: " . $lookup[$_REQUEST['Dish1']]);