You don't need to use a function; just use the ternary operator.
$var = (isset($_GET["marke"])) ? "?marke=" . $_GET["marke"] . "&farbe=gelb" : "?farbe=gelb";
Question
I need to insert
<?php if (isset($_GET["marke"])) { echo "?marke="; echo $_GET["marke"]; echo "&farbe=gelb"; } else { echo "?farbe=gelb"; } ?>
into a variable.
But of course
<?php
$var = if (isset($_GET["marke"])) { echo "?marke="; echo $_GET["marke"]; echo "&farbe=gelb"; } else { echo "?farbe=gelb"; }
?>
<?php
echo $var;
?>
isn't working ^^ What would the right Code look like?
Solution 2
You don't need to use a function; just use the ternary operator.
$var = (isset($_GET["marke"])) ? "?marke=" . $_GET["marke"] . "&farbe=gelb" : "?farbe=gelb";
OTHER TIPS
You must initialize the var and concatenate the strings:
$var = "?";
if (isset($_GET["marke"])) {
$var .= "marke=" . $_GET["marke"] . "&";
}
$var .= "farbe=gelb";
And then:
echo $var;
If you're just trying to set data to a variable if GET is set, and if not set to something else, use this:
if (isset($_GET["marke"]))
{
$var = "?marke=".$_GET['marke']."&farbe=gelb";
}
else
{
$var = "?farbe=gelb";
}
But remember to validate GET data before using it with mysql etc, to make sure it's what you allow.
Assuming that you want to save the output into a variable, and then do something before outputting the variable again, you can do something like this:
<?php
$var = if (isset($_GET["marke"])) { echo "?marke="; echo $_GET["marke"]; echo "&farbe=gelb"; } else { echo "?farbe=gelb";
echo <<<_END
<html>
//write something here
</html>
_END;
echo $var;
?>
Hope that helps!