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,016 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