2

Hello so I have a piece of code:

if($request['txt_!'] != "") {
  $randl1_1 = mt_rand(100000, 999999);
} else {
  $randl1_1 = '';
}

And when I convert it to a ternary operator:

$randl1_1 = ($request['txt_1'] != "") ? mt_rand(100000, 999999) : '';

What if I will add some in my if? Like,

if($request['txt_!'] != "") {
  $randl1_1 = mt_rand(100000, 999999);
  someFunction();
} else {
  $randl1_1 = '';
}

Is it possible in a ternary operator?

1
  • @Rizier123 Yes it is working, but just want to ask if there is something like that :D Commented Jul 31, 2015 at 0:36

2 Answers 2

6

It's possible, but it would make the use of a ternary less useful as it would clutter it (especially if you wanted to keep it on a single line). If you had it in the expression as the RHS, its return value would also be assigned to $randl1_1.

If someFunction() returned something truthy, then...

$randl1_1 = ($request['txt_1'] != "") ? someFunction() && mt_rand(100000, 999999) : '';

If it didn't you could use ||. But as you can see, this is ugly. If someFunction() relies on $randl1_1, well, then you have worse problems. :)

In your second case, I would use the more verbose example that you cited. You want your code to communicate to yourself and others clearly its intent.

Trying to shoehorn everything into a ternary is a bad practice.

Sign up to request clarification or add additional context in comments.

3 Comments

It's possible Can you please give a demo for this?
Your example which you show is different to OP's last code block. In OP's code the function neither has to return something nor do you have to concatenate it or something like this with the mt_rand() call.
@Rizier123 I realise that, that's why I wrote the things describing how it would require some assumptions. Either way you cut it, it's not going to be good code. Just trying to show how it could be possibly done.
3

You can't put multiple statements into the parameters of the ternary operator. You can use the comma operator to evaluate multiple expressions, though:

$rand1_1 = ($request['txt_1'] != "") ? (someFunction(), mt_rand(100000, 999999)) : '';

However, the comma operator returns its last operand. If you want to execute something after computing the value you want to assign, it won't work, e.g.

$rand1_1 = ($request['txt_1'] != "") ? (mt_rand(100000, 999999), someFunction()) : '';

This will set $rand1_1 to the value returned by someFunction(), not the random value. You'd have to save the random value in a variable:

$rand1_1 = ($request['txt_1'] != "") ? ($temp = mt_rand(100000, 999999), someFunction(), $temp) : '';

All this extra clutter makes the ternary really hard to read, negating the value of using it instead of a regular if statement.

3 Comments

Thank you sir. Will do the normal if else then
Ah. Finally the answer which I thought of :) The first sentence says everything!
Oops, unlike most other languages patterned after C, PHP doesn't have the comma operator. So this answer is all wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.