Reputation: 325
I want to print the same page's name differently based on a certain variable.
Here is a corresponding code.
$metaTitle ="'if($variable=='input'){ title#1 }else { title#2 };'";
And the produced meta title is lately used in the same file to create the page title (<title></title>
)
But it keeps producing the title like
if($variable=='input'){ title#1 }else { title#2 };
(the whole if statement as a whole. It does not recognize the if statement. It considers the statement as a plain text.)
What did I do wrong in the sentence??
Upvotes: 2
Views: 37482
Reputation: 480
Because you just assign $metaTitle a STRING "'if($variable=='input'){ title#1 }else { title#2 };'" and it's not a runable statement
you should do like this
if ($variable=='input') {
$metaTitle = "title#1";
} else {
$metaTitle = "title#2";
}
or simply use Ternary Operator
Upvotes: 3
Reputation: 3312
Try this instead -
if($variable=='input')
{
$metaTitle = 'title#1';
}
else
{
$metaTitle = 'title#2';
}
Upvotes: -1
Reputation: 30103
Use ternary operator "?:":
$metaTitle = ($variable=='input')? "title#1" : "title#2";
The first part is the condition:
($variable=='input')
The second is the result when condition is true:
"title#1"
The third is the result when condition is false:
"title#2"
Source http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
Upvotes: 16
Reputation: 1442
The simplest and most basic solution is to set the title variable inside the if statement.
if($variable=='input'){
$metaTitle = 'title#1';
} else {
$metaTitle = 'title#2';
}
Upvotes: 0