What is a function?

PHP & MySQL

In programming a function is a bit of code that waits around until its service is called upon. When called it performs a pre-programmed task. This task can be anything.

A function often requires an input such as a variable and often returns an output.

For example a simple function to add up two numbers would require the input of number A and number B (the two numbers). It would do the math and return a total figure, C.

Programming languages come with a lot of built in functions but you can also create your own.

 

How do you force a number to have 2 decimal points in PHP?

PHP & MySQL

The function you need is called number_format().

Using number_format (9876.54321, 2) would return 9876.54.

Just change the second number (2) to change the number of decimals.

This is great for money figures and cleaning up long strings of numbers after the decimal point.

 

How do you store decimal numbers in MySQL?

PHP & MySQL

There are various ways to store decimal numbers in MySQL but the simplest is probably to make the Datatype = float and the Length or Len = 10,2.

10 is the total number of digits. 2 is the number of digits following the decimal point.