开发者

Declare a PHP variable with length as we do in database char(10)

开发者 https://www.devze.com 2023-01-25 04:07 出处:网络
Can we declare a variable with fixed length in PHP? I\'m not asking about trimming or by putting condition do substring.

Can we declare a variable with fixed length in PHP?

I'm not asking about trimming or by putting condition do substring.

Can we declare variable just like database char(10).

The reason I'm asking am doing an export process, PHP export data to DB.

In DB I have a field with size 100, and I'm passing a fiel开发者_运维技巧d with length 25, using PHP.

When I look in DB, it's showing some extra space for that field.


Maybe it's your database that is the problem.

The CHAR datatype will always fill up the remaining unused characters when storing data. If you have CHAR(3) and pass 'hi', it will store it as 'hi '. This is true for a lot of relational database engines (MySQL, Postgres, SQLite, etc.).

This is why some database engines also have the VARCHAR datatype (which is variable, like the name says). This one doesn't pad the content with spaces if the data stored in isn't long enough.

In most cases, you are looking for the VARCHAR datatype. CHAR is mostly useful when you store codes, etc. that always have the same length (e.g.: a CHAR(3) field for storing codes like ADD, DEL, CHG, FIX, etc.).


No, a string in PHP is always variable length. You could trim the string to see if extra space is still passed to your DB.


Nope. PHP has no provision to limit string size.

You could simulate something in an object using setter and getter variables, though, throwing an error (or cutting off the data) if the incoming value is larger than allowed.


No, but I really don't think you're having a problem with php. I think you should check your DB2 configuration, perhaps it automatically completes strings with spaces... How much spaces are added? Are they added before? After?


As others have said: No.

I don't understand how it would help anyway. I'm not familiar with DB2 but it sounds like if you have extra spaces, they are either coming in the variable (and thus it should be trimmed) or DB2 does space padding to make the value have 100 characters. If your input is only 25 characters long then if it is doing space padding, it seems it would do it anyway.


If you want to store variable length strings in DB2 then go with VARCHAR, if you always want the same length for each string in the column, define the exact length using CHAR (for postal codes, for instance).

Details on character strings is available here: http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0008470.html with a good summary:

Fixed-length character string (CHAR)

All values in a fixed-length string column have the same length, which is determined by the length attribute of the column. The length attribute must be between 1 and 254, inclusive.

Varying-length character strings There are two types of varying-length character strings:

  • A VARCHAR value can be up to 32,672 bytes long.
  • A CLOB (character large object) value can be up to 2 gigabytes minus 1 byte (2,147,483,647 bytes) long.

Of course it then gets more detailed, depending on what sort of encoding you're using, etc... ( like UTF-16 or UTF-32 )

0

精彩评论

暂无评论...
验证码 换一张
取 消