astralaaron Posted February 21, 2010 Share Posted February 21, 2010 I understand print returns a value, echo doesn't making it slightly faster.Just wondering if anyone has a practical example of when print() could possibly return false?$example = "";if(print($example)) echo 'printed';else echo 'not printed';I can't get print to return false.. trying to understand it for whatever reason, just to learn.EDIT: i just tried this and thought for sure it would break the loop after the 3rd echo but no. <?php$darse = array('a','b','c');for($i = 0; $i < 1000; $i++){ if(print($darse[$i])) continue; else break;}echo 'loop ended'; Link to comment Share on other sites More sharing options...
Synook Posted February 21, 2010 Share Posted February 21, 2010 http://www.php.net/manual/en/function.print.php Return ValuesReturns 1, always. Link to comment Share on other sites More sharing options...
astralaaron Posted February 21, 2010 Author Share Posted February 21, 2010 lol - why is this even in the language? Link to comment Share on other sites More sharing options...
boen_robot Posted February 21, 2010 Share Posted February 21, 2010 lol - why is this even in the language?Perl compatibility, I think. Link to comment Share on other sites More sharing options...
jeffman Posted February 21, 2010 Share Posted February 21, 2010 For one thing, you can use shorthand like this:$x = 12;$x == 5 || print ('no')Which admittedly is a Perl-kind-of thing to do.Quiz: why does having a return value make this syntax legal? Link to comment Share on other sites More sharing options...
astralaaron Posted February 22, 2010 Author Share Posted February 22, 2010 Quiz: why does having a return value make this syntax legal?been staring at it like a deer in headlights, got me - but i want to know Link to comment Share on other sites More sharing options...
jeffman Posted February 22, 2010 Share Posted February 22, 2010 Logical operators evaluate expressions that can be evaluated as Boolean values. The && doesn't instruct print() to execute. The print expression executes on its own when control moves past the &&. If the thing to the right of && cannot be evaluated as true or false, you throw a runtime error.So an expression with a return value works. An expression without a return value does not work.I really wish break and continue statements had return values in the same way. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.