
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP program to find if a number is present in a given sequence of numbers
To find if a number is present in a given sequence of numbers, the code is as follows −
Example
<?php function contains_in_sequence($val_1, $val_2, $val_3) { if ($val_1 == $val_2) return true; if (($val_2 - $val_1) * $val_3 > 0 && ($val_2 - $val_1) % $val_3 == 0) return true; return false; } $val_1 = 11; $val_2 = 99; $val_3 = 2; print_r("Is the number present in the sequence? "); if (contains_in_sequence($val_1, $val_2, $val_3)) echo "Yes, it is present in the sequence"; else echo "No, it is not present in the sequence"; ?>
Output
Is the number present in the sequence? Yes, it is present in the sequence
A function named ‘contains_in_sequence’ checks to see if two values are same, and if they are equal, the function returns true. If the difference between the two values multiplied by a third value is greater than 0 and the difference between the values divided by the third value gives a reminder as 0, the function returns true, otherwise, returns false. Values are assigned to the three variables and the function is called by passing these values to the function. Relevant output is displayed on the console.
- Related Articles
- PHP program to check if a given number is present in an infinite series or not
- Find if the given number is present in the infinite sequence or not in C++
- Program to find out if a BST is present in a given binary tree in Python
- Program to find out if a linked list is present in a given binary tree in Python
- PHP program to find the sum of odd numbers within a given range
- Java program to check if a Substring is present in a given String
- Python Program to check if a substring is present in a given string.
- C# program to check if a Substring is present in a Given String
- Java program to find if the given number is a leap year?
- PHP program to find the numbers within a given array that are missing
- Program to find sum of given sequence in C++
- Program to find out the number of special numbers in a given range in Python
- C++ Program to Generate Randomized Sequence of Given Range of Numbers
- How to check if a given number is a Fibonacci number in Python Program ?
- C++ Program to Find the Longest Prefix Matching of a Given Sequence

Advertisements