I'm very new to PHP and this is throwing me for a loop.
So I understand some basic PHP IF statements, such as "if contains" "more than" but when it comes to my example; I'm a little confused.
The code below is for the SEO Title Tag. Currently it just echo's the following example:
The Hobbit Poster #1 - Movie Posters
How would I go about creating an IF Statment where if movie_name and poster_name doesn't exist then it just echo's "Movie Posters"
<title><?php echo $movie['movie_name']; ?> <?php echo $poster['poster_name']; ?> - Movie Posters</title>
Think of it another way:
IF (you have a movie_name OR a poster_name) show the DASH
<title><?php
$separator='';
if (isset($movie['movie_name'])) { echo $movie['movie_name'].' '; $separator='- '; }
if (isset($poster['poster_name'])) { echo $poster['poster_name'].' '; $separator='- '; }
echo $separator;
?>Movie Posters</title>
Option 1:
<title><?php echo (isset($movie['movie_name'])) ? $movie['movie_name'] : ''; ?> <?php echo (isset($poster['poster_name'])) ? $poster['poster_name'] : ''; ?> - Movie Posters</title>
Option 2:
$movie_name = (isset($movie['movie_name'])) ? $movie['movie_name'] : '';
$poster_name = (isset($poster['poster_name'])) ? $poster['poster_name'] : '';
<title><?php echo $movie_name; ?> <?php echo $poster_name; ?> - Movie Posters</title>
There are a bunch of ways to do this. Hopefully these point you in the right direction.
Since it appears we're all posting our own proposed solutions here, here's my explanation. Build up a string of everything you have, and then trim up any excess whitespace. Basically builds on @John Conde's method... he pretty much nailed it first time around. This is just a different way of looking at how PHP can be used.
// If either are set, echo a dash
if (isset($movie['movie_name']) || isset($poster['poster_name'])) {
$title = $movie['movie_name'];
$title .= ' ';
$title .= $poster['poster_name'];
$title .= ' - '
}
$title .= 'Movie Posters'
echo trim($title)
Also note that you can use the following syntax for echoing variables:
<span>Random number: <?= rand() ?></span>
<small><?= $text_to_go_in_small ?></small>