Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast as int or leave as string?

Tags:

php

mysqli

I'm wondering if there is any reason to cast my variable, which is returned from MySQL as a string, as an int. I'm updating some code and netbeans wants me to use === instead of ==. The problem is that === cares as much about the content as it does the type.

I had: if($user['user_state']==1){...}

So I change it to this: if($user['user_state']==="1"){...}

or I can do this: if((int)$user['user_state']===1){...}

It doesn't make a difference to me if I've got an int or str. I certainly won't be doing any math with this particular variable. And since I'll have to rewrite my conditionals in any case, I'd rather do it the right way.

So, I think my question is what is best practice? Or is this one of those wonderful questions whose answers will end happy marriages like single vs double quotes?

like image 829
jonlink Avatar asked Mar 10 '14 02:03

jonlink


People also ask

Can integer be cast to string?

Method 1: Using toString Method of Integer Class The Integer class has a static method that returns a String object representing the specified int parameter. The argument is converted and returned as a string instance. If the number is negative, the sign will be preserved.

What function converts cast a string into an integer or int?

To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed. The general syntax looks something like this: int("str") .

What happens if you cast int to char?

Typecasting int to char This means that you can't produce exact representation of an integer into a character (i.e. 65 => '65'), rather you will be having its ASCII code in output (i.e. 65=> 'A').

Can you cast int to string C++?

The next method in this list to convert int to string in C++ is by using the to_string() function. This function is used to convert not only the integer but numerical values of any data type into a string. The to_string() method is included in the header file of the class string, i.e., <string> or <cstring>.


1 Answers

The underlying reason for Netbeans to suggest such a thing is likely this:

"1abc" == 1 // true

Strengthening the comparison by applying an (int) cast and using === will satisfy the editor and purists, but since you would typically trust your database schema, the above cast won't be necessary and you can use a loose comparison instead.

To sum it up, although strict comparisons are a good practice in general it's often considered overzealous when working with external data such as databases and (to a lesser extent) posted data.

That said, it still leaves the matter of making Netbeans ignore these "issues" in a sensible way; having special instructions littered in your code is not a great situation either.

like image 157
Ja͢ck Avatar answered Sep 16 '22 20:09

Ja͢ck