note 129444 added to function.trim
| From: | zoad_aish@gmail.com | Date: | Mon, 29 Apr 2024 11:04:28 +0000 |
| Subject: | note 129444 added to function.trim | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221309@lists.php.net to get a copy of this message | ||
I wanted to bring to your attention a peculiar behavior in PHP that might catch some of us off
guard, particularly when dealing with string manipulation.
When using the trim() function in PHP with a variable assigned as null, the result might not be what
you expect. Instead of returning null, trim() will actually return an empty string "".
Here's a quick example to illustrate:
$txt = null;
var_dump($txt); // Outputs: NULL
$txt = trim($txt);
echo "<br>";
var_dump($txt); // Outputs: string(0) ""
As you can see, even though the initial value of $txt is null, after applying trim(), it becomes an
empty string "".
This behavior might seem counterintuitive at first, so it's important to keep it in mind,
especially when handling user input or dealing with data that might contain null values.
Understanding these nuances can save us from unexpected bugs and make our code more robust.
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.131 (proxied: 183.177.126.130)
----
Manual Page -- https://php.net/manual/en/function.trim.php
Edit -- https://main.php.net/note/edit/129444
Del: integrated -- https://main.php.net/note/delete/129444/integrated
Del: useless -- https://main.php.net/note/delete/129444/useless
Del: bad code -- https://main.php.net/note/delete/129444/bad+code
Del: spam -- https://main.php.net/note/delete/129444/spam
Del: non-english -- https://main.php.net/note/delete/129444/non-english
Del: in docs -- https://main.php.net/note/delete/129444/in+docs
Del: other reasons-- https://main.php.net/note/delete/129444
Reject -- https://main.php.net/note/reject/129444
Search -- https://main.php.net/manage/user-notes.php