aBe
aBe

Reputation: 77

how to set php variable to another variable when first one is empty then echo

I am trying to create a title from page variables using this.

<?php if $page_title = $page_title ?: $page_name'  - Blah.com'; { ?>
<?php } else { ?>
<?php echo $page_title . " - Blah.com" ; ?>
<?php } ?>

When added to page, all I get is a blank page once I open it in a browser. Not sure if I am doing this right.

I want it to use the page_title variable and some text after it but if page_title is empty I want it to use the page_name instead. Can this be done with shorter code?

Thanks for any help

Upvotes: 0

Views: 144

Answers (1)

Justin Taddei
Justin Taddei

Reputation: 2266

I think this is what you want:

<?php
  echo (isset($page_title)) ? $page_name . ' - Blah.com' : $page_title . ' - Blah.com' ;
?>

PHP ternary operator syntax is as follows:

$variable = condition ? if true : if false

Upvotes: 1

Related Questions