Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAndrew mwendo
5,712 Pointshelp with trimming guys
Challenge Task 2 of 2
Use the trim function to remove blank spaces and verify that the value is not an EMPTY word
<?php
session_start();
$word1 = htmlspecialchars($_SESSION['word'][1]);
if (!isset($_SESSION['word'][1])) {
header('location: /play.php?p=1');
exit;
}
$word2 = htmlspecialchars($_SESSION['word'][2]);
if (!isset($_SESSION['word'][2])) {
header('location: /play.php?p=2');
exit;
}
$word3 = htmlspecialchars($_SESSION['word'][3]);
if (!isset($_SESSION['word'][3])) {
header('location: /play.php?p=3');
exit;
}
$word4 = htmlspecialchars($_SESSION['word'][4]);
if (!isset($_SESSION['word'][4])) {
header('location: /play.php?p=4');
exit;
}
$word5 = htmlspecialchars($_SESSION['word'][5]);
if (!isset($_SESSION['word'][5])) {
header('location: /play.php?p=5');
exit;
}
include 'inc/header.php';
echo '<h1>My Treehouse Story</h1>';
echo '<p>There once was a(n) ' . $word1;
echo ' programmer named ' . $word2;
echo '. </p>';
echo '<p>This ' . $word3;
echo ' programmer used Treehouse to learn to ' . $word4;
echo ' the ' . $word5 . '.</p>';
echo ' <a class="btn btn-default btn-lg" href="#" role="button">Save Story</a>';
echo ' <a class="btn btn-default btn-lg" href="play.php" role="button">Play Again</a>';
echo ' <a class="btn btn-default btn-lg" href="index.php" role="button">Other Stories</a>';
include 'inc/footer.php';
3 Answers
Benjamin Larson
34,055 PointsWithin your IF conditionals, you need to add an OR statement to also check if a trimmed version of the word is empty.
You can use PHP's built-in empty()
and trim()
functions.
<?php
// This should be added to your existing conditionals with an OR clause
empty(trim($_SESSION['word'][1]))
As a side note, if you examine the code you wrote for the first task, you'll notice there's a lot of repetition for each of the word variables, with very little changing in each block. You could refactor this code to use a for loop, iterating through the number of words to greatly reduce the amount of code. Imagine if you had 100 words? It would get pretty annoying to copy and paste that so many times and hope you don't make a mistake changing one of the values. And then with task 2, you have to again make a change for every word. Using a for loop, you would only have to make a minor change in 1 spot to modify the number of iterations. Once you pass the challenge with the code as you have it, consider restarting it and trying to do it with a for loop and feel free to ask any questions.
Andrew mwendo
5,712 Pointsthis is what I did it won't pass..
<?php session_start(); $word1 = htmlspecialchars($_SESSION['word'][1]); if (!isset($_SESSION['word'][1])) { empty(trim($_SESSION['word'][1])) header('location: /play.php?p=1');
exit;
} $word2 = htmlspecialchars($_SESSION['word'][2]); empty(trim($_SESSION['word'][2])) if (!isset($_SESSION['word'][2])) { header('location: /play.php?p=2'); exit; } $word3 = htmlspecialchars($_SESSION['word'][3]); empty(trim($_SESSION['word'][3])) if (!isset($_SESSION['word'][3])) { header('location: /play.php?p=3'); exit; } $word4 = htmlspecialchars($_SESSION['word'][4]); empty(trim($_SESSION['word'][4])) if (!isset($_SESSION['word'][4])) { header('location: /play.php?p=4'); exit; } $word5 = htmlspecialchars($_SESSION['word'][5]); empty(trim($_SESSION['word'][5])) if (!isset($_SESSION['word'][5])) { header('location: /play.php?p=5'); exit; }
include 'inc/header.php';
echo '<h1>My Treehouse Story</h1>';
echo '<p>There once was a(n) ' . $word1; echo ' programmer named ' . $word2; echo '. </p>'; echo '<p>This ' . $word3; echo ' programmer used Treehouse to learn to ' . $word4; echo ' the ' . $word5 . '.</p>';
echo ' <a class="btn btn-default btn-lg" href="#" role="button">Save Story</a>'; echo ' <a class="btn btn-default btn-lg" href="play.php" role="button">Play Again</a>'; echo ' <a class="btn btn-default btn-lg" href="index.php" role="button">Other Stories</a>';
include 'inc/footer.php';
Benjamin Larson
34,055 PointsAs I mentioned before, you'll need to use the OR operator (||
`).
So you should be checking:
//pseudocode
IF (the given session variable is not set) OR (the trimmed variable is empty) {
}
If either case is true, then redirect with the header() function. Try to think through the problem and what the code is doing rather than rush to copy-and-paste code in order to pass the challenge and move on. I'm happy to answer PHP questions, but please try to explain your reasoning for the code you provide. A good idea for questions is listing what you expected your code to do vs. what it's actually doing or what error messages are telling you.
Andrew mwendo
5,712 PointsI am happy to that you answer but there is something I am not getting I tried too many times can u do an example on one session this is what I dd
<?php session_start(); $word1 = htmlspecialchars($_SESSION['word'][1]); if (!isset($_SESSION['word'][1]) ( ||) empty(trim($_SESSION['word'][1]){ header('location: /play.php?p=1');
exit;
} $word2 = htmlspecialchars($_SESSION['word'][2]);
if (!isset($_SESSION['word'][2])(||)empty(trim($_SESSION['word'][2]){ header('location: /play.php?p=2'); exit; } $word3 = htmlspecialchars($_SESSION['word'][3]); if (!isset($_SESSION['word'][3])(||)empty(trim($_SESSION['word'][3]) { header('location: /play.php?p=3'); exit; } $word4 = htmlspecialchars($_SESSION['word'][4]); if (!isset($_SESSION['word'][4]) (||)empty(trim($_SESSION['word'][4]){ header('location: /play.php?p=4'); exit; } $word5 = htmlspecialchars($_SESSION['word'][5]); if (!isset($_SESSION['word'][5])) (||)empty(trim($_SESSION['word'][5])){ header('location: /play.php?p=5'); exit; }
include 'inc/header.php';
echo '<h1>My Treehouse Story</h1>';
echo '<p>There once was a(n) ' . $word1; echo ' programmer named ' . $word2; echo '. </p>'; echo '<p>This ' . $word3; echo ' programmer used Treehouse to learn to ' . $word4; echo ' the ' . $word5 . '.</p>';
echo ' <a class="btn btn-default btn-lg" href="#" role="button">Save Story</a>'; echo ' <a class="btn btn-default btn-lg" href="play.php" role="button">Play Again</a>'; echo ' <a class="btn btn-default btn-lg" href="index.php" role="button">Other Stories</a>';
include 'inc/footer.php';
Tatenda Anesu Madharani
6,953 PointsThe correct answer :)
<?php session_start(); $word1 = htmlspecialchars($_SESSION['word'][1]); $word2 = htmlspecialchars($_SESSION['word'][2]); $word3 = htmlspecialchars($_SESSION['word'][3]); $word4 = htmlspecialchars($_SESSION['word'][4]); $word5 = htmlspecialchars($_SESSION['word'][5]);
for ($i = 1; $i <= 5; $i++) { $tmp = trim($_SESSION['word'][$i]); if (empty($tmp)) { if ($i == 1) { header('location:play.php'); exit; } else { header('location:play.php?p='.$i); exit; } } }
include 'inc/header.php';
echo '<h1>My Treehouse Story</h1>';
echo '<p>There once was a(n) ' . $word1; echo ' programmer named ' . $word2; echo '. </p>'; echo '<p>This ' . $word3; echo ' programmer used Treehouse to learn to ' . $word4; echo ' the ' . $word5 . '.</p>';
echo ' <a class="btn btn-default btn-lg" href="#" role="button">Save Story</a>'; echo ' <a class="btn btn-default btn-lg" href="play.php" role="button">Play Again</a>'; echo ' <a class="btn btn-default btn-lg" href="index.php" role="button">Other Stories</a>';
include 'inc/footer.php';
Ole Vรธlund Skov Mortensen
27,842 PointsOle Vรธlund Skov Mortensen
27,842 Pointsdude, why do you not use a for-loop