如何在echo语句php中使用if和declare变量

I have a condition within echo statement like this, how to adjust it to make it working:

echo "<option value="http://localhost/myproject/index.php?if(empty($_GET['view'])){echo "view=main-content";}else{$view=basename($_GET['view']);echo "view=".$view;}"></option>";

Many thanks

is this your expected output?

$d =  "<option value='http://localhost/myproject/index.php?";
if(empty($_GET['view']))
{
$d .= "view=main-content";
}
else
{
$view=basename($_GET['view']);
$d .="view=".$view;
};
$d .="'>Testing</option>";
echo "<select>". $d."</select>";

I noticed that you are using two times view, threfore if you condition enters the else your final url would be something like: ?view=whateverview=whatever2 which is clearly wrong.

$view = (empty($_GET['view']) ? 'main-content' : basename($_GET['view']));

echo "<option value='http://localhost/myproject/index.php?view=" . $view . "'></option>";

If you would like to add more parameters to the URL you'll need to use &.

You could pass it to a variable, and concatenate it with the rest of your echo statement.

if(empty($_GET['view'])){ 
    $res = 'main-content';
} else {
    $res = basename($_GET['view']);
}

echo '<option value="http://localhost/myproject/index.php?view=' . $res . '"></option>';

And since no one else posted it:

echo '<option value="http://localhost/myproject/index.php?view='
   . (empty($_GET['view']) ? 'main-content' : basename($_GET['view'])
   . '"></option>';