9

Why does

print "$str is " , ispalindrome($str) ? "" : " not" , " a palindrome\n" 

print "madam is a palindrome", but

print "$str is " . ispalindrome($str) ? "" : " not" . " a palindrome\n"

prints ""?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123

1 Answers1

19

The conditional operator (? :) has higher precedence than the comma, but lower than the period. Thus, the first line is parsed as:

print("$str is " , (ispalindrome($str) ? "" : " not"), " a palindrome\n")

while the second is parsed as:

print(("$str is " . ispalindrome($str)) ? "" : (" not" . " a palindrome\n"))
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
jwodder
  • 46,316
  • 9
  • 83
  • 106