php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34409 concatenating a "\n" to a single character string produces incorrect output
Submitted: 2005-09-07 15:50 UTC Modified: 2005-09-16 01:00 UTC
From: dskiles at docfinity dot com Assigned:
Status: No Feedback Package: Strings related
PHP Version: 5.0.5 OS: Windows XP sp2
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dskiles at docfinity dot com
New email:
PHP Version: OS:

 

 [2005-09-07 15:50 UTC] dskiles at docfinity dot com
Description:
------------
I've been running into a problem where a single character string will turn into a '?' character when I attempt to concatenate it or interpolate it with a single "\n" character.

If I use a multi-character string or multiple "\n" characters I don't get '?' characters anymore.

I'm running this from the console.

Reproduce code:
---------------
<?php

class Environment {
//todo:  add persistance

	const ALLPAIRS = 0;
	const EXHAUSTIVE = 1;

	///factors is a multidimenstional associative array.
	///the key for the main array is the factor.  The value of that key is a traditional array of values
	private $factors;
	
	///FactorMaps are 1:1 concrete representations made out of the factors
	private $factorMaps;
	
	///Mode states how FactorMaps are made from the factors
	///Currently, allpairs and exhaustive are supported
	private $mode;
	
	public function __construct($mode=Environment::ALLPAIRS) {
		$this->factors = array();
		$this->factorMaps = array();
		$this->mode = $mode;
	}
	
	public function addFactorValues($factor, $values) {
		if (!isset($this->factors[$factor])) {
			$this->factors[$factor] = array();
		}
		$this->factors[$factor] = array_merge($this->factors[$factor], $values);
		//todo:  update the factormaps if any factors were added
	}
	
	public function getFactorValues($factor) {
		return $this->factors[$factor];
	}
}

?>

/////////////////////////////////////////////

<?php
	require('../Environment.php');
	
	$enviro = new Environment();
	$enviro->addFactorValues('test', array('a', 'b', 'c', 'd'));
	foreach($enviro->getFactorValues('test') as $val) {
		echo("$val\n");
	}
	
?>

Expected result:
----------------
a
b
c
d

Actual result:
--------------
????

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-07 16:14 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip

And can't you really come up with a shorter reproduce script?

 [2005-09-07 16:17 UTC] tony2001@php.net
Works just fine here.
Also, line endings on windows are \r\n, not \n. 
 [2005-09-07 16:28 UTC] dskiles at docfinity dot com
I tried this with the latest CVS and got the same result.

Single character strings:
????

MultiCharacter strings:
aa
bb
cc
dd

Multiple Newlines:
a

b

c

d
 [2005-09-07 17:13 UTC] scottmacvicar at ntlworld dot com
I can't reproduce this.

[scott@devbox www]# php -v
PHP 5.0.6-dev (cli) (built: Sep  5 2005 18:38:22)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.5, Copyright (c) 1998-2004 Zend Technologies
[scott@devbox www]# php bugtest.php
a
b
c
d
 [2005-09-07 17:21 UTC] dskiles at docfinity dot com
Scott,
I noticed that you were using a UNIX shell, so I fired up Cygwin and tried the same thing on my box.

Cygwin:
a
b
c
d

Windows Shell:
????
 [2005-09-07 17:24 UTC] tony2001@php.net
Try this code:

<?php
var_dump("a\n");
var_dump("a\r\n");
?>
 [2005-09-07 17:29 UTC] dskiles at docfinity dot com
I get this result back:

string(2) "a
"
string(3) "a
"

Does PHP not offer a universal "\n", like Python or Java?
 [2005-09-07 17:38 UTC] scottmacvicar at ntlworld dot com
Try this then, though i can't see why it woudl end up one character. Are you sure your using the latest CVS version?

Check php -v

echo("$val" . PHP_EOL);
 [2005-09-07 17:43 UTC] dskiles at docfinity dot com
PHP 5.1.0-dev (cli) (built: Sep  7 2005 12:38:38)
Copyright (c) 1997-2005 The PHP Group
Zend Engine v2.1.0-dev, Copyright (c) 1998-2005 Zend Technologies

With PHP_EOL:
a
b
c
d
 [2005-09-08 07:35 UTC] sniper@php.net
In your original script, if you change the line with echo to this:

$a = "$val\n"; var_dump($a);

What is the output?

 [2005-09-16 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC