php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #60694 print() results in bad logic
Submitted: 2012-01-10 02:26 UTC Modified: 2012-01-11 02:54 UTC
From: MarkAndrewSlade at gmail dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.8 OS: CentOS release 5.4 (Final)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: MarkAndrewSlade at gmail dot com
New email:
PHP Version: OS:

 

 [2012-01-10 02:26 UTC] MarkAndrewSlade at gmail dot com
Description:
------------
Unlike echo, the print construct is allowed inside logic clauses.  If it is 
reached (not short-circuited), it will cause the rest of that clause to be true.  
Tested with 5.3.8 and PHP 5.3.9RC5-dev, both with default configure.



Test script:
---------------
<?php

echo (false && print('')) ? "Fail\n" : "Pass\n";
echo (print('') && false) ? "Fail\n" : "Pass\n";
echo (print('') && false && false) ? "Fail\n" : "Pass\n";
echo ((print('') && false) && true) ? "Fail\n" : "Pass\n";
echo ((print('') && false) && false) ? "Fail\n" : "Pass\n";

?>

Expected result:
----------------
The word "Pass" five times.

Actual result:
--------------
The middle three fail.  The first and last pass, and are included to demonstrate 
the limits of the bug.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-01-10 10:24 UTC] phpmpan at mpan dot pl
Invalid interpretation of the code caused by misleading parenthenses.
1. false && print ('')
   -> false && (print (''))
   -> false && 1
   -> false
2. print('') && false
   -> (print (('') && false))
   -> (print false)
   -> 1
   -> true
3. print('') && false && false
   -> (print ((('') && false) && false))
   -> (print false)
   -> 1
   -> true
4. (print('') && false) && true
   -> ( print (('') && false) ) && true
   -> (print false) && true
   -> 1 && true
   -> true
5. (print('') && false) && false
   -> (print (('') && false) && false
   -> (print false) && false
   -> 1 && false
   -> false

I believe that documentation for all language constructs (`echo`, `include`, `print`, ...) should explicitly discourage use of parenthenses around arguments. They're very misleading.
 [2012-01-10 22:00 UTC] MarkAndrewSlade at gmail dot com
Oh, I see what happened.  I agree with your recommendation.  The documentation 
says they are "not required" (implying optional), and they are used in the 
actual example.  I normally use echo, and without parens, so I just kinda 
randomly came across this.

I'm not sure if you were saying the parser's interpretation was invalid or mine, 
but in case the latter and this is considered correct parsing, the documentation 
should be updated to reflect that it's "print <string>", without parens.  
Developers can deduce for themselves that "print ('foo')" is allowed, but the 
semantics will be clearer.
 [2012-01-11 02:50 UTC] phpmpan at mpan dot pl
Your interpretation was invalid. This is a common pitfall. The documentation is very misleading, since it states that parenthenses are optional. This suggests that they're a part of the language construct itself and work like parenthenses in a function call. Unfortunely they aren't. They're interpreted as a part of an expression that is later passed to `print` (`echo`, `include` and similar). They have nothing to do with "function" itself. A nice example that shows this issue is the following snippet:
  print(string) TRUE;

I have posted a request (#60698) to change documentation.
 [2012-01-11 02:54 UTC] MarkAndrewSlade at gmail dot com
Roger that.  Thanks for clarifying.  Closing ticket.
 [2012-01-11 02:54 UTC] MarkAndrewSlade at gmail dot com
-Status: Open +Status: Closed -Type: Bug +Type: Documentation Problem
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Mar 08 06:01:23 2021 UTC