Chapter 23 More with Decision Control Structures

23.1 Simple Exercises with Decision Control Structures

Exercise 23.1-1 Is it an Integer?

Write a PHP script that prompts the user to enter a number and then displays a message indicating whether the data type of this number is integer or real.

Solution

It is well known that a number is considered an integer when it contains no fractional part. In PHP, you can use the (int) casting operator to get the integer portion of any real number. If the user-provided number is equal to its integer portion, then the number is considered an integer.

For example, if the user enters the number 7, this number and its integer portion, (int)(7), are equal.

On the other hand, if the user enters the number 7.3, this ...

Get PHP and Algorithmic Thinking for the Complete Beginner now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.