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 trial

PHP PHP Basics Daily Exercise Program True PHP

Lorenzo Leva
Lorenzo Leva
12,262 Points

True PhP chalange.

Hi, I dont know what I'm doing wrong in this chalange. Please help me. link: https://teamtreehouse.com/library/php-basics-2/daily-exercise-program/true-php

index.php
<?php
$a = 5;
//Place your code below this comment

$booleanOne = true;

$booleanTwo = $a === "5";

?>

1 Answer

The problem with your code is that you havn't included the if statement like this:

<?php
$a = 5;
//Place your code below this comment
$isBoolean = true;

if($a === '5'){
  $isIdentical = true;
} else {
  $isIdentical = false;
}
?>

If you where trying to use the ternary if statement then they must look like this variable = condition ? if-true : if-false

Example (This code will pass the challenge) :

<?php
$a = 5;
//Place your code below this comment

$isBoolean = true;
$isIdentical = $a === '5' ? true : false;

?>
Tobias Karlsson
Tobias Karlsson
2,751 Points

The problem with his code is actually improper naming of variables. The logic is perfectly fine and this works.

<?php
$a = 5;
//Place your code below this comment

$isBoolean = true;
$isIdentical = $a === "5";
?>

If you only want to return or get the value out of a boolean expression you should probably never use if-statements nor the shorthand ternary statement.

All three examples are equivalent (pseudo code):

1. variable = expression
2. variable = expression ? true : false
3. if (expression) variable = true else variable = false
Lorenzo Leva
Lorenzo Leva
12,262 Points

Thank you really much.

Alex Forseth
Alex Forseth
8,017 Points

When did we learn the "? true : false;" part? I get that its a boolean but wtheck does "? true : false" mean? I have not yet seen syntax that looks like that in this course.

I'm not sure if they do show this form of if statement on the PHP course but you can see it used here in the C# course (languages share a lot of the same principles). You can also see it here on the PHP docs