I am having confusion in if statement
I have like
if(hello==0 || bye==0)
{
echo 'error';
}
but if
hello==1 || bye==0 OR hello==0 || bye==1
then it should echo 'fine';
is their any process by which i can put this type of condition in if statement ??
EDIT
$numbers1=0;
$numbers2=0;
if(numbers1==0 || numbers2==0){
$percentage1 = ($numbers2 / ($numbers2 + $numbers1)) * 100;
$parcentage2 = ($numbers1 / ($numbers2 + $numbers1)) * 100;
}
This is why i can do fine clause as main statement
Warning: Division by zero
It looks like you want:
if($hello==1 || $bye==1)
{
echo 'fine';
}
else
{
echo 'error';
}
Oh, you could just do
if (($numbers1 + $numbers2) != 0)
echo 'fine';
else
echo 'error';
to make sure the result of $numbers1 and $numbers2 isn't zero before dividing by it.
Look carefully at your conditions.
This works if there are no other possible values other than 0 or 1 (best to use a bool in this case probably)
if($hello==0 || $bye==0)
{
echo 'error';
}
else
{
echo 'fine';
}
If you want to guard against a null value then just use the isset() in your conditional.
Looks like you want to avoid division by zero which can occur when both $number1 and $number2 are zero.
if (0 === $number1 && 0 === $number2)
{
// division by zero
}
else
{
// perform your calculation
}
One thing you did not make clear: Could either or both $number1 and $number2 be negative? If yes, then you might want to consider the condition where the sum of the numbers are zero (as per #sbat's suggestion).
if( $numbers1!= 0 || $numbers2 != 0){
$percentage1 = ($numbers2 / ($numbers2 + $numbers1)) * 100;
$parcentage2 = ($numbers1 / ($numbers2 + $numbers1)) * 100;
}
else
{
echo 'error';
}
Note that you are missing the dollar sign before numbers1 and numbers2 variables in your if statement. Good Luck.
Related
im trying to create a script that generates a number and depending on the number generated it prints something specific but it's not working properly, Heres the code:
<?php
for($zz = 1; $zz <= 20; $zz++) {
$rangen = rand(1,100);
$a = (1 <= 0) && (0 <= 7);
$b = (8 <= 0) && (0 <= 17);
echo ("<br>".$rangen . "<br>");
if($a) {
echo "a";
} elseif ($b) {
echo "b";
} else {
echo "c";
}
}
?>
The error is that it keeps printing "c" no matter what the number is.
If anyone could help that would be great, thanks.
Your conditions are all wrong. Your comparing the same numbers and never using $rangen, this is why you obtain the same result each time.
1 <= 0 and 8 <= 0 will always return false which is why you always go to the else statement.
I'm reading data from a text file and performing some basic math. This is how things should work:
// no warning, expected result N/A
$dividend = 100;
$divisor = 0.0;
if (!empty($dividend) && !empty($divisor))
{
$quotient = $dividend/$divisor;
} else {
$quotient = 'N/A';
}
echo $quotient;
This is how things are actually happening.
// yeilds division by zero warning
$dividend = 100;
$divisor = '0.0';
if (!empty($dividend) && !empty($divisor))
{
$quotient = $dividend/$divisor;
} else {
$quotient = 'N/A';
}
echo $quotient;
I'm getting a division by zero warning when a value in the text file is read as '0.0', which empty() sees as non-empty, when it actually empty.
What's the best way to test that '0.0' is actually 0?
Typecast $divisor to float: $divisor = (float)'0.0';
You'll want to cast to float (or use floatval()) in your condition statement. This leaves your values untouched in case you need other data from your variables, such as trailing text:
$dividend = 100;
$divisor = '0.0';
if ((float)$dividend && (float)$divisor) //both are non-zero
{
$quotient = $dividend/$divisor;
} else { //one or the other are zero
$quotient = 'N/A';
}
echo $quotient;
However consider only checking $divisor for zero.
Cast it to an int;
$divisor = (int) '0.0';
i think quotient should be zero if divisior is zero.
So you get a result in the following operations instead a N/A.
I always resolve it like this:
$quotient = ($divisor == 0) ? 0 : ($divident/$divisor);
it should work for "0.0", too.
I have the following calculation:
$this->count = float(44.28)
$multiple = float(0.36)
$calc = $this->count / $multiple;
$calc = 44.28 / 0.36 = 123
Now I want to check if my variable $calc is integer (has decimals) or not.
I tried doing if(is_int()) {} but that doesn't work because $calc = (float)123.
Also tried this-
if($calc == round($calc))
{
die('is integer');
}
else
{
die('is float);
}
but that also doesn't work because it returns in every case 'is float'. In the case above that should'n be true because 123 is the same as 123 after rounding.
Try-
if ((string)(int) $calc === (string)$calc) {
//it is an integer
}else{
//it is a float
}
Demo
As CodeBird pointed out in a comment to the question, floating points can exhibit unexpected behaviour due to precision "errors".
e.g.
<?php
$x = 1.4-0.5;
$z = 0.9;
echo $x, ' ', $z, ' ', $x==$z ? 'yes':'no';
prints on my machine (win8, x64 but 32bit build of php)
0.9 0.9 no
took a while to find a (hopefully correct) example that is a) relevant to this question and b) obvious (I think x / y * y is obvious enough).
again this was tested on a 32bit build on a 64bit windows 8
<?php
$y = 0.01; // some mambojambo here...
for($i=1; $i<31; $i++) { // ... because ...
$y += 0.01; // ... just writing ...
} // ... $y = 0.31000 didn't work
$x = 5.0 / $y;
$x *= $y;
echo 'x=', $x, "\r\n";
var_dump((int)$x==$x);
and the output is
x=5
bool(false)
Depending on what you're trying to achieve it might be necessary to check if the value is within a certain range of an integer (or it might be just a marginalia on the other side of the spectrum ;-) ), e.g.
function is_intval($x, $epsilon = 0.00001) {
$x = abs($x - round($x));
return $x < $epsilon;
};
and you might also take a look at some arbitrary precision library, e.g. the bcmath extension where you can set "the scale of precision".
You can do it using ((int) $var == $var)
$var = 9;
echo ((int) $var == $var) ? 'true' : 'false';
//Will print true;
$var = 9.6;
echo ((int) $var == $var) ? 'true' : 'false';
//Will print false;
Basically you check if the int value of $var equal to $var
round() will return a float. This is because you can set the number of decimals.
You could use a regex:
if(preg_match('~^[0-9]+$~', $calc))
PHP will convert $calc automatically into a string when passing it to preg_match().
You can use number_format() to convert number into correct format and then work like this
$count = (float)(44.28);
$multiple = (float)(0.36);
$calc = $count / $multiple;
//$calc = 44.28 / 0.36 = 123
$calc = number_format($calc, 2, '.', '');
if(($calc) == round($calc))
die("is integer");
else
die("is not integer");
Demo
Ok I guess I'am pretty late to the party but this is a alternative using fmod() which is a modulo operation. I simply store the fraction after the calculation of 2 variables and check if they are > 0 which would imply it is a float.
<?php
class booHoo{
public function __construct($numberUno, $numberDos) {
$this->numberUno= $numberUno;
$this->numberDos= $numberDos;
}
public function compare() {
$fraction = fmod($this->numberUno, $this->numberDos);
if($fraction > 0) {
echo 'is floating point';
} else {
echo 'is Integer';
}
}
}
$check= new booHoo(5, 0.26);
$check->compare();
Eval here
Edit: Reminder Fmod will use a division to compare numbers the whole documentation can be found here
if (empty($calc - (int)$calc))
{
return true; // is int
}else{
return false; // is no int
}
Try this:
//$calc = 123;
$calc = 123.110;
if(ceil($calc) == $calc)
{
die("is integer");
}
else
{
die("is float");
}
you may use the is_int() function at the place of round() function.
if(is_int($calc)) {
die('is integer');
} else {
die('is float);
}
I think it would help you
A more unorthodox way of checking if a float is also an integer:
// ctype returns bool from a string and that is why use strval
$result = ctype_digit(strval($float));
I have a game development project and I have specific character one of the skill of the character is that it has a 10% chance of doubling its attack.
Question: How can I trigger it?
I hope i understand ure question:
<?php
$random = rand(1,10);
if(($random == 1) || ($random == 2) || ($random == 3))
$value += $value;
?>
Here you have a 30% chance to hit 1, 2 or 3.. and if its hit then your value gets doubled.
Ok now for your Update u just need a 10% chance? But okay:
<?php
$random = rand(1,10);
if($random == 1)
$value += $value;
?>
function doubleHit($percentChance = 30)
{
if (mt_rand(1,100) <= $percentChance) {
return true;
}
return false;
}
var_dump(doubleHit(35)); // will return either true / false
Note that this is only pseudorandom. Also note that this is faster / better than rand().
For example this code will double the value theoretically one in three times? It's hard for me to understand you.
$value = 200;
if (rand(1,3)===1) {
$value*=2;
}
Or maybe this, for percents:
$value = 200;
$percent = 30;
$chance = rand(1, 100);
if ($chance <= $percent) {
$value *= 2;
}
function rawtransform{
if ($raw>=500 && $raw<=550){
$score= 1;
}
if ($raw>=550 && $raw<=600){
$score= 2;
}
if ($raw>=600 && $raw<=650){
$score= 3;
}
if ($raw>=700 && $raw<=750){
$score= 4;
}
if ($raw>=750 && $raw<=800){
$score= 5;
}
if ($raw>=800 && $raw<=850){
$score= 6;
}
if ($raw>=850 && $raw<=900){
$score= 7;
}
if ($raw>=900 && $raw<=950){
$score= 8;
}
if ($raw>=950 && $raw<=1000){
$score= 9;
}
}
This seems very basic and not very well coded. (I am only learning php )
Can anyone offer a better way of doing this? maybe a single if statement. I think there is a way just cant get my head round it.
Thanks
How about just using math?
function rawtransform($raw) {
$score = (int)($raw/50)-9;
}
You may want to add a range check for the input, though.
You can create a list of conditions, and loop through the and apply the if.
$conditions = array(
array(500, 550, 1), // greater than value, lesser than value, assignment value
array(550, 600, 2),
array(650, 700, 3) // add the rest of the conditions
);
foreach($conditions as $condition) {
if($raw >= $condition[0] && $raw <= $condition[1]) {
$score = $condition[2];
}
}
if ($raw >= 500 && $raw<= 1000){
$score = ceil(($raw-500)/50);
}
You can use if...else constructs.
if ($raw>=500 && $raw<=550){
$score= 1;
}
elseif ($raw>=550 && $raw<=600){
$score= 2;
}
elseif ($raw>=600 && $raw<=650){
$score= 3;
}
That way, if the script encounters a size of, say, 575 it won't even bother to go through the following conditions.
In your given example, the script can be simplified by:
function rawtransform($raw) { $score = floor(($raw - 450)/50); return $score; }
Your logic seems to be that you are subtracting 450 from raw, then dividing it by 50 and rounding down to the nearest whole number. (There are problems in your implementation however, as if raw is a factor of 50, it will meet the requirements for two of the if statements and there is a condition missing for when it falls between 650 and 700.)
You could do this as follows:
floor(($raw-450)/50)