Java: integer variables

This entry is part 5 of 54 in the series Learn Java

In a programming language, the usage of a variable is very similar to variable in Mathematics.  We can assign variable with a particular value.

The example below will make use of 3 variables to to perform a simple mathematical function.

package javaapplication4;

public class JavaApplication4 {

    public static void main(String[] args) {
        
        int a = 10;
        int b = 15;
        int total;
        total = a + b;
        System.out.println(total);
    }
}

In this program, we first create integers a and b.  We then sum the variables and assign the sum to the variable total.

integer

 

PHP Form Validation: Numeric elements

This entry is part 3 of 7 in the series PHP Form Validation

To ensure that a submitted value is an integer or floating-point numeric number, use the conversion functions intval() and floatval().

They give you the number (integer or floating point) inside a string, discarding any extraneous text or alternative number formats.

You can use the following code to check for integer:

if ($_POST['age'] != strval(intval($_POST['age']))) {
    $errors[] = 'Please enter a valid age.';
}

And to check for floating-point number:

if ($_POST['price'] != strval(floatval($_POST['price']))) {
    $errors[] = 'Please enter a valid price.';
}