how can I check for numbers only from -10 negative to +10 positive?
This is what I have, but I think it's not safe:
if(isset($_POST['number']) && ctype_digit($_POST['number']) && $_POST['number']>=-10 && $_POST['number']<=10){
//do something
}
and the form:
Input a number between -10 and 10: <input type="text" name="number" size="5" />
if( isset($_POST['number'])) {
$num = intval($_POST['number']);
if( $num >= -10 && $num <= 10) {
// do something
}
}
There are other ways, but that one will work. Anything that can't be converted to a number will be treated as zero. If this is not desired behaviour, add:
&& "".$num == $_POST['number']
To that inner IF statement, to ensure that no non-numeric characters were removed from the input.
Check whether a variable is a number including zero and negative values
$x = '-22';
if (isNumber($x, ['zero','negative']))
echo 'Yes';
else
echo 'No';
isNumber($x, $includes=[])
{
if (is_int($x)) {
if ($x === 0) {
if (in_array('zero', $includes))
return true;
} elseif ($x < 0) {
if (in_array('negative', $includes))
return true;
} else
return true;
} elseif (is_string($x)) {
if ($x == '0') {
if (in_array('zero', $includes))
return true;
} elseif ($x[0] == '-') {
if (in_array('negative', $includes))
return ctype_digit(substr($x, 1));
} else
return ctype_digit($x);
}
}
Related
<?php
if (isset($_GET['number'])){ // Loaded for first time?
if(isValid($_GET['number'])){
$isPrime = true;
// Please enter code here
// I have tried the codes here but idk why it cannot seem to work
$i = $_GET(['number']);
if ($number == 1) {
$isPrime = false;
return $isPrime;
}
for ($j = 2; $i <= $number/2; $i++){
if ($number % $i == 0)
$isPrime = false;
return $isPrime;
}
return $isPrime;
// End of code
if ($isPrime) {
echo "<p>".$i." is a prime number!</p>";
} else {
echo "<p>".$i." is not prime.</p>";
}
} else{
// User submitted something which is not a positive whole number
echo "<p>Please enter a positive whole number.</p>";
}
}
// check if the given number is a valid numeric value
// round() rounds a floating point value
function isValid($number) {
if(is_numeric($number) && $number > 0
&& $number == round($number, 0)) {
return true;
}
else {
return false;
}
}
?>
I have tried the above codes, however, I have encountered issues. Please help me out thank you! The codes I have tried are within the // Please enter code here to // End of code
When I run the php file the above image will be shown. The user have to type a number and it will check whether or not the user input is a prime number when user clicks on the "Go!" button.
You had many mistakes.
So I edited the whole code.
here it is:
<?php
if (isset($_GET['number'])) { // Loaded for first time?
if (isValid($_GET['number'])) {
$isPrime = true;
// Please enter code here
// I have tried the codes here but idk why it cannot seem to work
$i = $_GET['number'];
if ($i == 1) {
$isPrime = false;
}
for ($j = 2; $j <= $i / 2; $j++) {
if ($i % $j == 0)
$isPrime = false;
}
// End of code
if ($isPrime) {
echo "<p>" . $i . " is a prime number!</p>";
} else {
echo "<p>" . $i . " is not prime.</p>";
}
} else {
// User submitted something which is not a positive whole number
echo "<p>Please enter a positive whole number.</p>";
}
}
// check if the given number is a valid numeric value
// round() rounds a floating point value
function isValid($number)
{
if (is_numeric($number) && $number > 0
&& $number == round($number)) {
return true;
} else {
return false;
}
}
?>
<form action="index.php" method="get">
<input type="number" name="number">
<input type="submit" value="Go!">
</form>
I'm trying to check if a date is real, it must return true, and if not, it must return false.
It does not seemes to work when I write 35-02-2012 (Date format dd-mm-yy) it return true, but I was expecting false, I do not know where I'm wrong.
below is my function
function isItRealDate($date) {
if ($date == '') {
return false;
} else {
$rxDatePattern = '/^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/'; //Declare Regex
$dtArray = preg_match($rxDatePattern, $date); // is format OK?
if ($dtArray == '0') {
return false;
} else {
$tableau_date = explode('-', $date);
//Checks for dd-mm-yyyy format.
$dtMonth = $tableau_date[1];
$dtDay = $tableau_date[0];
$dtYear = $tableau_date[2];
if ($dtMonth < 1 || $dtMonth > 12) {
return false;
} elseif ($dtDay < 1 || $dtDay > 31) {
return false;
} elseif (($dtMonth == 4 || $dtMonth == 6 || $dtMonth == 9 || $dtMonth == 11) && $dtDay == 31) {
return false;
} elseif ($dtMonth == 2) {
$isleap = ($dtYear % 4 == 0 && ($dtYear % 100 != 0 || $dtYear % 400 == 0));
if ($dtDay > 29 || ($dtDay == 29 && !$isleap)) {
return false;
} else {
return true;
}
}
}
}
}
anykind of help will be much appreciated
If you want something that just works, use checkdate().
As suggested by Ibrahim, use:
function isItRealDate($date) {
if(preg_match("#^(\d{1,2})\-(\d{1,2})\-(\d{1,4})$#", $date, $match)){
//regex could match 99-99-9999 - checkdate will take care.
return checkdate($match[2], $match[1], $match[3]);
}
return false;
}
This will work for ANY (valid) date between 1-1-1 and 31-12-9999
My html document has the following form:
<form action="PrimeNumber.php" method="post">
Enter a number to determine if it is a prime number: <input type="text" name="numb" size="10">
<input type="submit" value="Check for Primeness">
</form>
Edit: Using a different code now but can't get it to echo a statement no matter what I do. Anyone know how I can make it echo is a prime number or is not.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['numb'])) {
$num = $_POST['numb'];
function isPrime($num) {
if($num == 1) {
return false;
}
if($num == 2) {
return true;
}
if($num % 2 == 0) {
return false;
}
for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
if($num % $i == 0)
return false;
}
return true;
}
}
}
?>
simple and easy to understand code, and working as well. do little changes according to your requirement, like assign input text value to $a variable and you good to go.
$a = 51;
for($i=2; $i < $a; $i++){
if($a % $i == 0){
echo "Numberis not prime.";
break;
}
else{
echo "Number is not prime.";
break;
}
}
A simple function to check is prime number:
function is_prime($p) {
return ($p > 1) && (($p%2 >= 1) && ($p%3 >= 1) && ($p%5 >= 1)) || in_array($p, [2,3,5]);
}
function checkPrime($num){
$isPrime = true;
for($i = 2; $i <= sqrt($num); $i++)
{
if($num % $i == 0)
{
$isPrime = false;
}
}
if($isPrime == true)
{
return "$num is prime number";
}else{
return "$num is not prime number";
}
}
echo checkPrime(29);
The output is
29 is prime number
For More Details
You may use the gmp package and correct your code because I got an output as 11 not being a prime number or use the following function as an alternative.
function isPrime($num) {
if($num == 1) {
return false;
}
if($num == 2) {
return true;
}
if($num % 2 == 0) {
return false;
}
for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
if($num % $i == 0)
return false;
}
return true;
}
More information is available at the following page.
A simple function to help you find out if a number is a prime number
<?php
function fn_prime($number) {
$i = 2; $result = TRUE;
while($i < $number) {
if(!($number%$i)) {
$result = FALSE;
}
$i++;
}
return $result;
}
?>
I am doing the following if else statement below but number (//1) and number (//4) get executed at the same time, I am finding it abit hard to understand why.
<?php
//1
if($a == 1 && count($b) == 0) {
// do this
}
//2
elseif ($a == 1 && count($b) > 0) {
// do that
}
//3
if($a== 0 && count($b) == 0) {
// do a different thing
}
//4
else {
// do the last thing
}
?>
I have done this and it works but i think the should be a more suitable way for not using elseif for this.
else if($a== 0 && count($b) > 0) {
// do the last thing
}
but number (//1) and number (//4) get executed at the same time
It's because you don't have else before the if on //3
//3
if($a== 0 &&
Change to elseif($a== 0 &&
At the moment you have two separate IF conditions
You're missing a closing brace after your first if.
Also, you have a weird operator inside your first condition : $$. Maybe you intended to type &&?
$a = 10;
if ($a == 5) {
echo 'ok';
} elseif ($a == 10) { // $a is equal to 10, so it executes;
echo 'not_ok';
}
if ($a > 20) {
echo 'ok_ok';
} else { // $a is not >20 so else statement executes
echo 'not_not';
}
final result: not_oknot_not
If you are performing such tests on one and the same assignee, but different values, you might not want to execute more than one?
I guess you need elseif where third block is if
if ($a == 5) {
echo 'ok';
} elseif ($a == 10) { // $a is equal to 10, so it executes and stops the block;
echo 'not_ok';
} elseif ($a > 20) {
echo 'ok_ok';
} else { // $a is not >20, but the block was stopped on first elseif
echo 'not_not';
}
produces not_ok
Even if you move the else statement after the first elseif block as was suggested
if ($a > 20) {
echo 'ok_ok';
}
will execute, and if it's true, it will produce result, which again will result in double result
You might want to do this...
if ($a == 1 && count($b) == 0) {
// do this
}
//2
elseif ($a == 1 && count($b) > 0) {
// do that
}
elseif ($a== 0 && count($b) == 0) {
// do a different thing
}
else {
// do the last thing
}
The reason they get executed at the same time is that... Well technically they're not executed at the same time since it's procedural, but they both get executed because they are both different if else conditions. If you want only 1 execution, you should combine them :)
This seems simple but I don't know why it doe snot work.
I need to write an if statement that
first, checks if it is numeric
second, if it is not between 1 and 10, issue errorA
third, if it is not between 20 and 30, issue errorB
fourth, it is not a number, issue errorC
If is not numeric and satisfies all the ranges, added to the database.
anyways, I am not sure about the if and while combination to satisfy this....
So far I have,
if numeric and satisfies ranges, add to database
else, issue errorC
How can I filter for error A and B?
if ( isset [some code...]) {
$a = ...;
$b = ...);
$c = ...;
if (preg_match('/^\d+$/',$b) && preg_match('/^\d+$/',$c) &&
((1 <= $b && 10 >= $b)) && ((20 <= $c && 30 >= $c))) {
$sql = "INSERT [some code...]
mysql_query($sql);
$_SESSION['success'] = $_POST['success'];
header('Location: index.php') ;
return;
} else {
$_SESSION['error'] = $_POST['error'];
header('Location: index.php') ;
return;
}
}
if (preg_match('/^\d+$/',$b) && preg_match('/^\d+$/',$c)) {
if (($b >= 1 && $b <= 10) && ($c >= 20 && $c <= 30)) {
echo "OK";
} else {
echo "not in range";
}
} else {
echo "not a number";
}