March 19, 2013

php difference between 1 and '1'

Question by Blaze Tama

I have struggle with this problem for hours. I have this method my model (codeigniter) :

public function get_umat($kelas1 = 0, $kelas2 = 0) {
    $this->db->select('*');
    $this->db->from('msumat');
    $this->db->join('mskelas', 'msumat.kelas_id = mskelas.kelas_id');

    if($kelas1 != 0)
    {
        echo $kelas1;
        var_dump($kelas1);
        $this->db->where('mskelas.kelas_id', $kelas1);
    }
    else if($kelas2 !=0)
    {
        echo '2';
        $this->db->where('mskelas.kelas_id', $kelas2);
    }
    else if($kelas1 != 0 && $kelas2 !=0)
    {
        echo '3';
        $this->db->where('mskelas.kelas_id BETWEEN $kelas1 AND $kelas2');
    }

    return $this->db->get();
}

EDIT :
The one that not working is in this line of code (taken from above) :

$this->db->where('mskelas.kelas_id', $kelas1);

Its not working when i called this method in my controller, like this :

$this->backend_m->get_umat($_POST['ddl_kelas1'], $_POST['ddl_kelas1']);

I get ‘1’(String) when i vardump the ($_POST['ddl_kelas1']

Then i try to change the parameter in the controller, but its still not working :

 $this->backend_m->get_umat(1, $_POST['ddl_kelas1']);

Desperately, i tried to change the parameter directly in the model, and its working :

public function get_umat($kelas1 = 1, $kelas2 = 0)

Whats going on here?i think it has something to do with the difference of 1 (int) and ‘1’ (String). Thanks 😀

Answer by Starx

Cast your variables as integer.

$this->backend_m->get_umat((int)$_POST['ddl_kelas1'], (int)$_POST['ddl_kelas1']);

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!