开发者

Any way to echo the value of a logical comparison in PHP?

开发者 https://www.devze.com 2023-02-19 05:46 出处:网络
for example can how can I properly write, $a = \'cat-dog\'; echo ((strpos($a, \'-\') !== false)); // and get true on 开发者_JAVA技巧screen.

for example can how can I properly write,

$a = 'cat-dog';
echo ((strpos($a, '-') !== false));
// and get true on 开发者_JAVA技巧screen.

Without having to use a separate variable to store the value.

Thank you so much!


You could use the function var_dump(); instead of echo. Have a look at the manual.

For example:

$a = 'cat-dog';
var_dump(strpos($a, '-') !== false);
//Would output: bool(true)


Use the ternary operator:

echo ((strpos($a, '-') !== false) ? "true" : "false");


$a = 'cat-dog';
echo (strpos($a, '-') !== false) 
   ? "true" 
   : "false";


Can't you just cast the result to string with (string)($AnyResult)?


instead of stacking operators and using ternary operator you have also possibility to write readable and maintainable code

$a = 'cat-dog';
$pos = strpos($a, '-');
if ($pos !== false) {
  echo "TRUE";
}

I see no rocket science in it though. just regular PHP operators, donno what made it trouble for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消