php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55822 elseif/else if opcode difference
Submitted: 2011-09-30 16:42 UTC Modified: 2020-04-09 16:03 UTC
Votes:4
Avg. Score:4.2 ± 1.3
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: jhansche at myyearbook dot com Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.8 OS: Linux
Private report: No CVE-ID: None
 [2011-09-30 16:42 UTC] jhansche at myyearbook dot com
Description:
------------
---
From manual page: http://www.php.net/control-structures.elseif
---
It is worth explaining the difference in opcode generation using "elseif" vs "else if" -- or at least the effect of using one over the other.

Specifically, I'm referring to using PHPUnit with XDebug, in order to get code coverage for some code.  What I've noticed is that using "else if", even if you fully test that branch, no code coverage is produced for that condition line (the code coverage report just skips over the "else if .." line, leaving one line unexecuted, while everything else is green).  Replacing that line with "elseif" instead, produces full green code coverage!

See the test script below, and the output from using vld (http://pecl.php.net/package/vld) to show the difference in opcode generation, wherein having the space between "else" and "if" causes the opcode to report the wrong line number, and therefore something like xdebug, which tracks what lines get executed, is unable to track the fact that the line was executed, and there is no code coverage.

The difference between "elseif" and "else if" should not cause unit testing code coverage reports to change behavior.  The only way to get proper code coverage with this bug is to force the developer to use "elseif".

Test script and results can also be seen at: https://php.privatepaste.com/f468504857

Test script:
---------------
space.php:
<?php
if($x)
{
  echo 0;
}
else if ($y)
{
  echo 1;
}
?>

nospace.php:
<?php
if($x)
{
  echo 0;
}
elseif ($y)
{
  echo 1;
}

$ php -dvld.active=1 space.php >space.vld
$ php -dvld.active=1 nospace.php >nospace.vld
$ diff -uN space.vld nospace.vld


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-08 21:35 UTC] frozenfire@php.net
I feel that discussion of the opcode is beyond the scope of that document. It's 
a very esoteric distinction, which would only serve to confuse, rather than to 
educate.

It would probably be a better idea to document this in XDebug or PHPUnit's 
manual, rather than PHP's. For the purposes of the execution of the code, this 
distinction is irrelevant.

Though, better yet, it might be worth opening a bug for XDebug, indicating that 
this trivial opcode distinction is impairing the ability to get proper code 
coverage.
 [2011-10-09 09:59 UTC] derick@php.net
@frozenfire: No need to open a bug (he already did that AFAIK), because it's not something in Xdebug that I can do anything about (as the opcodes are generated by PHP). It doesn't belong in PHP's docs though, but perhaps we can see whether we can change PHP itself to make this look similar.
 [2011-12-04 00:32 UTC] frozenfire@php.net
@derick: I agree that it would probably be sensible for PHP to conjoin else if 
into elseif, where it sees that opcode combination. I'll change the bug to that.
 [2011-12-04 00:32 UTC] frozenfire@php.net
-Type: Documentation Problem +Type: Feature/Change Request -Package: Documentation problem +Package: Scripting Engine problem
 [2020-04-09 16:03 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2020-04-09 16:03 UTC] cmb@php.net
This appears to have been resolved:
<https://3v4l.org/8KaqL/vld#output>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 06:01:29 2024 UTC