Doc #55822 [Com]: elseif/else if opcode difference

From: Date: Sat, 08 Oct 2011 21:35:36 +0000
Subject: Doc #55822 [Com]: elseif/else if opcode difference
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-7304@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=55822&edit=1

 ID:                 55822
 Comment by:         frozenfire@php.net
 Reported by:        jhansche at myyearbook dot com
 Summary:            elseif/else if opcode difference
 Status:             Open
 Type:               Documentation Problem
 Package:            Documentation problem
 Operating System:   Linux
 PHP Version:        5.3.8
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2011-09-30 16:42:57] 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



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=55822&edit=1


Thread (3 messages)

« previous php.doc.bugs (#7304) next »