Php convert empty string to null

I want to turn an empty input/ string into null, but it seems I can't get it work. Below is the class that handles $_POST,

class post 
{
    public $data = array[];

    public function get[$param, $default = null] {
        if [!isset[$this->data[$param]]] {//not set, return default
            return $default;
        }
        else if[isset[$this->data[$param]] && $default === '']{//empty string
            return null;
        }
        return $this->data[$param];
    }

}

For instance,

$post = new post[];
$test = $post->get['url',''];
var_dump[$test];

I get this,

string '' [length=0]

But I want null. Is it possible?

asked Sep 14, 2013 at 18:31

3

You probably don't need to add an argument:

$test = $post->get['url'];

The default for it is null after all.

Or else you could explicitly specify it:

$test = $post->get['url', null];

answered Sep 14, 2013 at 18:37

konsoleboxkonsolebox

68.2k11 gold badges96 silver badges103 bronze badges

0

In addition to @konsolebox's answer, you can also silently convert empty strings to NULLs inside the get[] method:

public function get[$param, $default = null] {
    if [$default === ''] {
        $default = null;
    }

    // ...

The advantage to this is that when your default is dynamic [passed in as a variable], and it happens to be an empty string, it still produces the desired effect:

$default = '';
$test = $post->get['url', $default]; // null

answered Sep 14, 2013 at 18:40

FtDRbwLXw6FtDRbwLXw6

27k13 gold badges67 silver badges104 bronze badges

1

  • b126
    Junior Member

  • Posts: 20
    Threads: 6
    Joined: Feb 2019

    Reputation: 0


I would like to convert all my POST values from '' to NULL automatically with a Codeigniter validation rule. Is it possible?
Here is my helper, which is working fine with text and numbers :

PHP Code:

function empty2null[$value]
{
 
   return $value === '' null $value;

Can I define it as a validation rule [prepping?] somewhere? How to do this?
Afterwards I would like to send it as a proper NULL value to Doctrine.

I tried to implement it as a classic rules, but this custom function is not triggered when the POST value is empty.

Thank you

  • b126
    Junior Member

  • Posts: 20
    Threads: 6
    Joined: Feb 2019

    Reputation: 0

02-27-2019, 01:30 PM
[This post was last modified: 02-27-2019, 02:24 PM by b126.]


[02-27-2019, 01:10 PM]php_rocs Wrote: @b126,

You should be able to use a PHP function such as isset[] [scroll down page to See Also for other suggestions- //php.net/manual/en/function.isset.php].

return isset[$value]? null : $value;

Hi,

Thank you but it’s not a question of isset[], or empty[] or ===‘’.

It’s a question on how to trigger this conversion automatically while posting the form data or during the validation.

Unfortunately, it seems that CI posts empty string if you do not put anything in your input field, like it is the case for optional fields for example.

So when a user leave a blank field for an optional input like « distance », the input->post[‘distance’] value posted is ‘’.

When you convert ‘’ to an integer in php, it becomes 0. 
You will then record 0 in your database instead of NULL.

The same is for empty strings. You will save ‘’ in your database instead of null.

In Laravel, you can use the ConvertEmptyStringsToNull middleware. 
In Symfony, I think you can use empty_data option.
So I wonder if there was an equivalent in Codeigniter.


@b126,

You could take the forms data array and determine if it has any '' values and if so then change it either NULL or 0.

  • b126
    Junior Member

  • Posts: 20
    Threads: 6
    Joined: Feb 2019

    Reputation: 0

02-27-2019, 04:11 PM
[This post was last modified: 02-27-2019, 04:13 PM by b126.]


At the end I think that I will override the post[] method of the CI_Input core class instead.

Quicker and easier than everything else.
Just one line of code to add.


That sounds like a good approach. I'd like to see the implementation.

  • b126
    Junior Member

  • Posts: 20
    Threads: 6
    Joined: Feb 2019

    Reputation: 0


[02-28-2019, 12:25 PM]dave friend Wrote: That sounds like a good approach. I'd like to see the implementation.

I did it and it’s working fine, with strings, and also with numeric values. 
Here is my /application/core/MY_Input.php

PHP Code:

class MY_Input extends CI_Input {
 
   public function post[$index NULL$xss_clean NULL]
 
   {
 
       $value$this->_fetch_from_array[$_POST$index$xss_clean];
 
       return $value === '' null $value;
 
       //return isset[$value] ? $value : [isset[$default_fail] ? $default_fail : NULL];
 
   }


[02-28-2019, 12:45 PM]b126 Wrote: I did it and it’s working fine, with strings, and also with numeric values. 
Here is my /application/core/MY_Input.php

PHP Code:

class MY_Input extends CI_Input {
 
   public function post[$index NULL$xss_clean NULL]
 
   {
 
       $value$this->_fetch_from_array[$_POST$index$xss_clean];
 
       return $value === '' null $value;
 
       //return isset[$value] ? $value : [isset[$default_fail] ? $default_fail : NULL];
 
   }

One thing to be aware of is your code introduces a bug when you ask CI_Input::post[] to return the entire $_POST array. i.e.

PHP Code:

$data $this->input->post[]; 

That might not be something you ever do, but future code maintenance or changes might run up against this. It's an easy thing to forget and a hard thing to point out to someone else coming to the project.

I think you have to either override _fetch_from_array[] - which is not as trivial as what you've done - or,  you could do the following

PHP Code:

public function post[$index NULL$xss_clean NULL]
{
 
   $data $this->_fetch_from_array[$_POST$index$xss_clean];
 
   if[is_array[$data]]
 
   {
 
       $output = array[];
 
       foreach[$data as $key => $value]
 
       {
 
           $output[$key] = $value === '' NULL $value;
 
       }
 
       return $output;
 
   }
 
   return $data === '' null $data;

Have you considered that you might want to trim the input in case somebody enters a string with empty spaces?

Is null or empty PHP?

is_null[] The empty[] function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null[] will return true only if the variable has the value NULL .

Is empty string null MySQL?

Learn MySQL from scratch for Data Science and Analytics In the above syntax, if you compare empty string[ ' '] to empty string[ ' '], the result will always be NULL.

Is empty in PHP?

PHP empty[] Function The empty[] function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.

How do you convert an empty string to a decimal?

It's not possible to convert an empty string to a number [decimal or not]. You can test for an empty string before trying the conversion or use decimal. TryParse[] - I think that was available in 1.1.

Chủ Đề