php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41176 User defined function return incorrect characters
Submitted: 2007-04-24 11:45 UTC Modified: 2007-04-25 08:39 UTC
From: ian at avionwbt dot co dot uk Assigned:
Status: Not a bug Package: CGI/CLI related
PHP Version: 5.2.1 OS: Win 2003 Web Edition IIS6
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ian at avionwbt dot co dot uk
New email:
PHP Version: OS:

 

 [2007-04-24 11:45 UTC] ian at avionwbt dot co dot uk
Description:
------------
The comma-separated return string from a user-defined function sometimes (about 20%, erratically) returns a pipe ('|') separated return string instead of a comma-separated string.

The function works through a two-dimensional array, applying scaling factors to some elements and returning the modified first-dimension array.  The example code below has been abbreviated by removing conditional (if-else) blocks for simplification.

This problem has been traced by writing the return out to a log file, clips of which are listed below.

This code worked fine in PHP 4, but became erratice after upgrade to PHP5.

Note also that following upgrade to PHP 5 that the code failed fatally until the "if(!function_exists('calcPrice')){" condition was added.

Reproduce code:
---------------
if(!function_exists('calcPrice')){
	function calcPrice($prod){
		global $vat;					
		global $cFactor;
		global $lessonData;

		$curr = $lessonData[$prod]['currentPrice'];
		$name = $lessonData[$prod]['name'];

		$v = ($curr*$lessonData[$prod]['netVATrate'])*0.01;
		$p0 = $cFactor * ($curr+$v);
		return (  "$prod,$p0,$curr,$name,0");	
		}
	}
}
foreach($lsnArray as $value){	# $lsnArray is external
$p = calcPrice($value);

# For reasons which escape me entirely, the 
# function calcPrice() periodically returns 
# a string with '|' separators instead of  ','  !!!!!! 

$item = str_replace( "|", ",",$p);    # work-around solution

$pricelist .= "&$value=$item";	      # Required URL-encoded output
		 
fwrite($LOG, "\r\nPricelist element: $value=$p");   # TRACE


Expected result:
----------------
Log file clip, achieved about 80% of instances (erratic):

Pricelist increment: INTR=INTR,0,0.00,Introduction to Tutor,0
Pricelist increment: OV01=OV01,11.75,10.00,Overview of the PRINCE2 Pricelist increment: OV02=OV02,15.275,13.00,Principles of PRINCE2,0
Pricelist increment: BC01=BC01,12.925,11.00,Business Case,0
Pricelist increment: OR01=OR01,16.45,14.00,Project Organisation,0
Pricelist increment: PR01=PR01,29.9625,25.50,Overview of PRINCE2 Processes,0
...
etc


Actual result:
--------------
Log file clip, actual about 20% of instances (erratic):

Pricelist increment: INTR=INTR|0|0|Introduction to Tutor|0
Pricelist increment: OV01=OV01|11.75|10|Overview of the PRINCE2 Method|0
Pricelist increment: OV02=OV02|15.275|13|Principles of PRINCE2|0
Pricelist increment: BC01=BC01|12.925|11|Business Case|0
Pricelist increment: OR01=OR01|16.45|14|Project Organisation|0
Pricelist increment: PR01=PR01|29.962|25.5|Overview of PRINCE2 Processes|0
...
etc


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-24 11:49 UTC] tony2001@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.


 [2007-04-25 08:35 UTC] ian at avionwbt dot co dot uk
This bug report is withdrawn by the originator.
Reason: Not a bug.
Explanation: 
1. Amongst many scripts, two subroutine functions with the same name existed, with slightly different output - one with comma separators, and one with '|' separators. These functions used different input data, but the same operations. 
2. The upgrade of PHP from 4 to 5 required the wrapping of user-defined functions with if(!function_exists('name'){...
else a fatal error was thrown (can't redefine functions).  
3. In PHP 4 the subroutine functions were redefined on each invocation.  In PHP 5 the current function depended on application sequencing, giving rise to different output formats.
4. Oops.
 [2007-04-25 08:39 UTC] tony2001@php.net
Not PHP problem -> bogus.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Aug 15 06:00:03 2025 UTC