-3

What will the following code print? print ‘’four’’ * 200;

8
  • 8
    Why not just try it?
    – balpha
    Jul 1, 2009 at 12:21
  • 2
    He's testing us to see how knowledgeable we are before asking his real question
    – Vinko Vrsalovic
    Jul 1, 2009 at 12:22
  • Why not read other questions and answers? There's a ton of knowledge here.
    – C. Ross
    Jul 1, 2009 at 12:24
  • I actually wanted to have a explanation for this, thanks for Conrad's comment. I needed to ask the question in a different manner ;-) Sorry Guys ;-)
    – Elitmiar
    Jul 1, 2009 at 12:27
  • The question is quite OK, it should just have read 'Why is "four" * 200 === 0 in PHP'
    – soulmerge
    Jul 1, 2009 at 12:28

3 Answers 3

7

It prints "0" alt text http://mywebprogrammer.com/images/soAnswer.PNG

The result is to the left of the second line.

To see why this is you can do a quick test echo (int)'four'; this will attempt to explicitly cast the string 'four' to an integer which since it is not an integer will technicaly fail, resulting in a 0 which of course is equal to FALSE. If you replace the 'four' with '4', still a string, you can properly cast it to an integer and it will produce the result of 800 in the case of your example ("print '4' * 200").

6

Yes it would print zero "0" indeed. The thing is PHP will type cast the string value to an integer. This would result in 0 (Zero); and if you times any value with zero you'll get zero.

Good Question Roland!

0
4

Since the string cannot be casted to a number, the multiplication with a string will result in 0.

1
  • Numeric strings can be casted to the integer they represent though.
    – Vinko Vrsalovic
    Jul 2, 2009 at 4:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.