I have this PHP code
What this code do is matching month count to numbers .
If condition is true echo result!
Result is displayed in table for each user.
$isT is count of months between 2 dates example 1,2,3 or 9
if($isT=='3'
OR $isT=='6'
OR $isT=='9'
OR $isT=='12'
OR $isT=='15'
OR $isT=='18'
OR $isT=='21'){
//echo something
}
What i want is make this numbers automatically generated
So it would be like:
if($isT==$generatednumber[$i]){
//echo something
}
i need numbers in this order 3 6 9 12 15. ..
basically +3 to the last number
First make sure its more than 0 then check if its multiple of 3.
if($isT >0 && ($isT % 3) == 0)){
//echo something
}
As i said modulus (%) is needed here:-
if(!empty($isT) && ($isT % 3) == 0)){
//echo something
}
Note:- this code will check:-
1.Your variable is set
2.Have some value
3.Is a multiple of 3.
Related
Is there a way for PHP to check if there is an number after the . in a variable?
for example: $x=4.5 and now i want PHP to check if its a round number or one with a number after the . like shown in $x. I made a script where i want PHP to devide my Variale by 2 and then check if the variable is now round or has a decimal space.
while($dezimal !== 0) {
$dezimal/2;
//here is where i need to check if $dezimal is round or not
floor($dezimal);
}
The modulo operator will tell you if a number is evenly divisible by another.
if ($dezimal % 2 === 0) {
// number divides evenly
} else {
// number has a remainder
}
I want to get my output like this
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
I am trying like this:
<?php
for($a=1; $a<=16; $a++)
{
for($b=$a; $b>=1; $b--)
{
echo "$b";
}
echo "<br>";
}
?>
The above code gives me the wrong output.
Let's debug.
You are starting from 1 in your outer loop and in your inner loop, you are going from $a till 1 times.
This doesn't comply with your requirements because we have to print an increasing sequence in each row.
You can also notice that every number in a row differs by 4.
So, logic would be like below:
Pseudocode:
rows = 4
starting_number = 1
loop from 1 to rows
number = starting_number
loop from 1 to 4 // since each row has 4 numbers
print number
number += 4
print new_line
starting_number++
Demo: https://3v4l.org/9YjIP
Im looking for someone to point me in the right direction to coding some statistical comparisons. Currently I query my database, and will get data back as follows:
main data set :
3,4,7,10,5,8,1,3,7
sets to compare with could be like this, and can have multiple sets.
4,5,6,9,10,2,3,4,6
Now i need to work out the difference between these two sets of data - for example, difference between 3-4 is 1. I then need to choose the biggest difference, the most agreed upon, and the lowest scoring.
How would you tackle coding this?
I would recommend to use the function array_walk(), passing the first array as the first parameter and the second array as the third, optional parameter. It would look like this:
<?php
$array_1 = array(3,4,7,10,5,8,1,3,7);
$array_2 = array(4,5,6,9,10,2,3,4,6);
// making a copy, because the callback function
// works on the actual value
$array_1_copy = $array_1;
echo '<pre>';
array_walk($array_1_copy, 'difference', $array_2);
echo "\nThe maximum difference is: ". max($array_1_copy);
echo "\nThe minimum difference is: ". min($array_1_copy);
echo '</pre>';
// the callback function, takes the 1st param as reference
function difference(&$value_1, $index_1, $array_2) {
$difference = abs($value_1 - $array_2[$index_1]);
echo "Difference between $value_1 and {$array_2[$index_1]} is $difference\n";
$value_1 = $difference;
}
?>
And the output for this code is:
Difference between 3 and 4 is 1
Difference between 4 and 5 is 1
Difference between 7 and 6 is 1
Difference between 10 and 9 is 1
Difference between 5 and 10 is 5
Difference between 8 and 2 is 6
Difference between 1 and 3 is 2
Difference between 3 and 4 is 1
Difference between 7 and 6 is 1
The maximum difference is: 6
The minimum difference is: 1
$max_diff = 0;
for ($i = 0; $i < (min(count($array1), count($array2));$i++){
if (($array1[$i]-$array2[$i]) > $max_diff ) $max_diff = $array1[$i]-$array2[$i];
}
echo $max_diff;
Something like that...Didn't actually tested it, but that's the idea.
I would like to get a person's weight, who is over 18 years old so very likely to be 100 pounds plus, so I want to check if the $_POST variable for the weight entered is 3 digits only. I have a form where the user enters their weight...
if (!is_numeric($weight)) {
echo "You did not enter a numeric weight.";
die;
}
Using the above to make sure it's numeric... but also want to verify the number is 3 digits.
Thank you
You can do:
is_numeric($weight) && ($weight > 99) && ($weight < 1000)
I create this in php for generate random numbers
> <?php
>
> $products=array("1","2","3","4","5","6","7","8","9","10");
>
> for ($i=0;$i<count($products);$i++) {
>
> $numbers=rand(0,count(products));
>
> print "".$products[$numbers]."<br>";
>
> } ?>
I try generate in bucle different numbers but always show me the same numbers 1212121212 and nothing more , how i can generate this string or array and for example finally show 2 3 4 1 5 6 7 9 8 10 , and if reload script other combination
ThankĀ“s Regards !!!
You forgot the $ in count($products). As a result, the parser is treating it as the string "products", which has a count() of 1. Therefore, the rand() function returns zero or one, which in your original array correspond to "1" and "2".
Try using shuffle,
$products=array("1","2","3","4","5","6","7","8","9","10");
shuffle($products);
foreach($products as $v)
echo $v;
Demo