Archive for the ‘data types’ Category

Fun with PHP data types

Thursday, February 2nd, 2012

All data stored by PHP is put into one of eight different types of buckets, technically called data types. The data type determines what operations can be carried out on that piece of data.

Four of the data types are scalar–they support only a single value. These are integer (a whole number), float (a number with a decimal point), string (a sequence of symbols) and boolean (0 or 1).

PHP also has two compound data types. One is an array, or a bounded collection of multiple values. The other is an object, or a collection of data that also contains properties and methods. Finally, PHP has two special data types. One is a resource, which is a reference to some external data source. The other, null, has only one value, null.

PHP is a loosely typed language, meaning the variables do not need to have their data types defined before they are used. If you code $sample = 4.5; then PHP will assume the the $sample variable is a floating point data type. It is totally up to you to make sure that you pass the correct data type to any operation (i.e. don’t ask PHP to multiply two text strings).

That said, PHP offers some tools to help get a handle on all your loose data types. PHP’s gettype() function can let you know what type of data type the provided variable is, i.e. gettype( $sample ). You can also change a data type with PHP settype(), i.e. settype ( $sample, “integer”) will return the value of 4. PHP tries to preserve as much of the original value as possible during the conversion process.

Finally, you can set a variable’s type even before it is assigned a value. This is called type casting. Here you place the name of the data type, in parenthesis, before the name of the variable. So “(float) $sample = 4.5″ will ensure that the $sample variable will be a floating point data type.

From the book




All mistakes are my own, however…-Joab Jackson





SQL: Constraining the Database

Friday, October 30th, 2009

Constraining what gets entered into a relational database is a good thing. It maintains the data integrity that is so important for database use. You don’t want users to enter the wrong types of information (I.e. letters instead of numbers)

My database class teaches that there are three different ways to constrain input for a database:

1. Declarative Integrity
2. Procedural Application Code
3. Business Procedure

Declarative Integrity means you put restraints directly in the table design. The constraints specify what input the database accepts. More on them below.

When you restrain by using the Procedural Application Code, you put the constraints not in the database tables, but in the programming logic that handles the input of the data (PL/SQL), with actions such as “triggers.”

Business rules or for those cases when you can not (easily) check the integrity of the data by computer, so you put the rules in the employee handbook (“Enter your real birth date, not a false birth date” would be a silly example).


Here’s some basics on Declarative Integrity. When you create a data table, you can add in constraints on what data is accepted. This can be done as part of a column definition, or at the end of the “create table” statement.

Here are the basic types of Integrity Constraints you can use, at least for Oracle databases:

NOT NULL: When an insert is made, a column defined as NOT NULL must be given some data. The NOT NULL declaration goes right after the data type. For instance, when creating the column with the DATE data type, you would add:

[NameOfColumn] DATE NOT NULL,

UNIQUE: UNIQUE requires each new value entered into that column be different from all those value entered before.

PRIMARY KEY: The PRIMARY KEY is the one column that identifies the column from all the others. As such it is considered UNIQUE, meaning each value entered will be different from all the other values entered. However, you don’t use the UNIQUE qualifier when declaring the PRIMARY KEY (it is implied). Here is an example:

[NameOfColumn] [DataType] PRIMARY KEY,

The PRIMARY KEY can be a composite of multiple column entries, which means each each key must be comprised of a unique combination of values from the participating columns. The composite key is defined at the end of the table creation statement, after the last column definition:

constraint [Name_Of_Composite_Primary_Key] PRIMARY KEY ([Name_of_1st_Participating_column], [Name_of_2nd_Participating_column], [...] )

FOREIGN KEY: a FOREIGN KEY uses as its domain of possible values a PRIMARY KEY from another table. This is written as a column definition:

constraint [Name_Of_Foreign_Key] foreign key ([Name_of_External_Column])

references [Name_Of_External_Table]([Name_of_External_Column])

Note, you can not refer to a table in another database, only to another table in the same database. But you can refer to the primary key even in the same table.

The references clause tells the database to delete the dependent row when the corresponding row in the parent table is deleted.

CHECK: The CHECK constraint allows you to specify only certain values can be inserted, as such:

[Name_Of_Column] [Datatype] Check([Name_Of_Column] [operator] [value]),

For example,

Stats VARCHAR2(2) CHECK (Stats => 0)

….means that any values entered for the Stats column must be 0 or higher.

Material taken from this book….



…As well as from a class I’m taking on database design. All mistakes are my own…–Joab Jackson





Javascript: Math data types

Sunday, June 29th, 2008

MATH DATA TYPES:

Javascript can do the basic mathematical operators ( +, – , /). Javascript can express more complex mathematical operations through a single math object. This is the square root:

x = 16;
y = Math.sqrt(x);
document.write(y);

(will display the answer “4″)

And here is the sine of x:

z = Math.sin(x);
document.write(z);

(will display the answer “-0.2879033166650653 “)

You can convert a number to a string:

a = x.toString(10);

(will display 16)

You can specify the base:

a = x.toString(16);

(10)

a = x.toString(2);

(10000)

–Joab Jackson