php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #2459 trim converts single newline into null
Submitted: 1999-10-06 21:03 UTC Modified: 1999-10-07 09:17 UTC
From: alderete at be dot com Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.12 OS: FreeBSD
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: alderete at be dot com
New email:
PHP Version: OS:

 

 [1999-10-06 21:03 UTC] alderete at be dot com
While working on some form validation code, I came across what seems like a corner case for the trim function. 

I have two address fields on my form, and to validate them, I concatenate them together with a newline in between, and then trim the result. Then I check to see if this is the empty string.

If both form fields are empty, the concatenation produces a single newline. Applying trim to that changes the variable to contain a single null [str (0)], instead of producing an empty string as expected.

Put another way, the result of the below code produces a string where sizeof() == 1, instead of 0.

Here's the sample code that triggers it for me:

$ADDRESS_1 = "";
$ADDRESS_2 = "";
$ADDRESS = trim ($ADDRESS_1 . "\n" . $ADDRESS_2);
if (ord ($ADDRESS) == 0) { 
	print ("Newline converted to null!");
};

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-10-07 09:17 UTC] andrei at cvs dot php dot net
trim() is behaving as expected. If a string containing only "\n"
is passed to it, the result is an empty string. You're confused
because you're trying to use sizeof() to get the size of the string.
Sizeof() is the same as count() so it returns 1 since the variable
has 1 value. You should be using strlen() to get the length of the
string.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 14 18:00:02 2026 UTC