Ternary Operator

Hi I am on lesson “Booleans and Comparison Operators” and doing the section on Ternary Operator.
link : https://www.codecademy.com/courses/learn-php/lessons/booleans-and-comparison-operators/exercises/ternary-operator?action=resume_content_item

This is the error I am getting.

Your function should return a string. We tested your function and expected a string return value but instead it returned a value of type NULL

This is my code:

<?php
namespace Codecademy;

function ternaryCheckout($num)
{
  $num <= 12 ? "express lane" : "regular lane";
  	
};


It says its not getting my return string. I thought this seemed like the right way to do this, but it is still giving me error. Maybe there is an issue with my syntax, that I am not seeing. Could someone help me with this?

1 Like

In your code block, add return to the front of the line.

3 Likes

Okay, that worked. Thank you. I’m confused why it needs a return at the start of the line.
I thought that the expressions are already returned once it determines whether the condition is true or false

2 Likes

As far as I know, PHP does not have explicit returns. The ternary you have there is not a statement, but an expression (yields a single value, in this case a string). return can only return an expression, not a statement. Once return is added, your code is completely correct.

return %expression%;
2 Likes

Okay I see yes, I got confused what the ternary actually does. Like you said I was thinking it was a statement. Thank you for explaining!

2 Likes