Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Another simple "if-else" form in PHP?

i read somewhere before, there is another way to perform the if-else statement, the code should be similar to this:

<?php
  $var = "stackoverflow";
  // Here is the if-else
  if(strlen($var) > 1) ? echo "TRUE" : echo "FALSE";
?>

I could only remember something like this, but it doesn't work, anyone knows how to write this 1 line if-else statement in php??

like image 993
jingleboy99 Avatar asked Feb 12 '26 16:02

jingleboy99


2 Answers

echo strlen($var) > 1 ? "TRUE" : "FALSE";

or

if (strlen($var) > 1) echo "TRUE"; else echo "FALSE";
like image 124
John Kugelman Avatar answered Feb 15 '26 04:02

John Kugelman


echo in php not inline operator. for this case need to use opeartor print

<?php
  $var = "stackoverflow";
  // Here is the if-else
  strlen($var) > 1 ? print("TRUE") : print("FALSE");
?>
like image 39
DARK_DIESEL Avatar answered Feb 15 '26 06:02

DARK_DIESEL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!