Home > Blockchain >  Which code can be used for Trim() 8 when data is null or empty (deprecated warning in php 8)
Which code can be used for Trim() 8 when data is null or empty (deprecated warning in php 8)

Time:10-06

Just a quick question about the deprecated trim($data) in php 8.x. when $data is null.

is this the best option to avoid the warning:

if (isset($data)) {$data = trim($data);}

I use trim very often to remove white spaces from an EXCEL-derived MySQL database. I wish a solution that can be applied with a search and replace procedure on my editor to all webpages to facilitate the task. THANKS

CodePudding user response:

Use this:

$data = trim($data ?? "");

The null coalescing operator will provide an empty string as the default value if $data is unset or null.

CodePudding user response:

You can cast your variable to ensure the type passed to the trim function

if (isset($data)) {$data = trim((string) $data);}

More about typecasting here

  • Related