Try this:
header ( 'Location: ' . ( $a == 1 ? 'http://google.com' : 'http://yahoo.com' ) );
Note that $a = 1
is an assignment and will always evaluate to true
. You need ==
to achieve comparison.
Question
I'm wondering if it's possible to replicate:
if ($a = 1) {
header('Location: http://google.com');
exit;
} else {
header('Location http://yahoo.com');
exit;
}
Using short hand? Maybe...
$a = 1 ? header('Location: http://google.com') : header('Location http://yahoo.com');
exit;
This does not seem to work, meaning I'm always redirected to google. =(
La solution 2
Try this:
header ( 'Location: ' . ( $a == 1 ? 'http://google.com' : 'http://yahoo.com' ) );
Note that $a = 1
is an assignment and will always evaluate to true
. You need ==
to achieve comparison.
Autres conseils
if ($a = 1) {
it should be
if ($a == 1) {
and
$a = 1 ?
should be
$a == 1 ?
$location= $a == 1 ? "http://google.com" : "http://yahoo.com";
header("Location: $location");
Also =
is assignment operator not a comparison operator. That should be ==
or ===
I would leave it as is.
Using a ternary here is terrible for readability - which code block will be easier and faster to understand for a new programmer inheriting your code?