I have the code :
function insertChamado($id, $area = 2)
{
if ($area != 2 && $area != 4)
$area = 2;
How Can I adjust this code to not accept 0 condition as show in the log bellow :
[12-May-2016 16:58:28 America/Sao_Paulo] id = 36445, area = 0
[12-May-2016 16:59:00 America/Sao_Paulo] id = 14635, area = 0
[12-May-2016 17:00:02 America/Sao_Paulo] id = 18599, area = 0
Just add a conditional to check for it... Not sure what is hard about it unless we are missing something.
function insertChamado($id, $area = 2)
{
if ($area == 0) die("Ruh-Rohh");
if ($area != 2 && $area != 4)
$area = 2;
}
Or if you expect it to be 2 if it is 0:
function insertChamado($id, $area = 2)
{
if (($area != 2 && $area != 4) || $area == 0) // Though || $area == 0 actually does nothing here as 0 already meets the previous condition.
$area = 2;
}
It occurs to me after the fact, that $area could never be 0 even in your original code as 0 != 2 and 0 != 4 thus $area = 2. I suspect an implementation issue, if this does not help I suggest you edit your ques to include more code.
Could be a scope issue, as you are not using a global $area and are not returning a value, the changed $area may not be breaking out of the function.
Try one of the se implementations:
Using global
$area = 0; // for testing only
function insertChamado($id)
{
global $area;
if ($area != 2 && $area != 4)
$area = 2;
}
Or using a return:
$area = insertChamado(0,0);
function insertChamado($id, $area = 2)
{
if ($area != 2 && $area != 4)
$area = 2;
return $area;
}
The incomplete code you supplied does not help as I have no idea what the implementation of id is.
After reading carefully your question i think that your best solution is a simple switch.
function insertChamado($id, $area = 2){
switch ($area) {
case 2:
echo "area equals 2\n";
break;
case 4:
echo "area equals 4\n";
break;
default:
echo "area is always 2 other wise\n";
}
}
insertChamado('id',0); // will output "area is always 2 other wise"
insertChamado('id'); // will output "area equals 2"
Related
I want to change the students marks to GP so I have code like this
$mark=80;
if($mark>=84.50 && $mark<=100){$GP = 4;}
if($mark>=79.50 && $mark<=84.49){$GP = 3.67;}
if($mark>=74.50 && $mark<=79.49){$GP = 3.33;}
if($mark>=69.50 && $mark<=74.49){$GP = 3;}
if($mark>=64.50 && $mark<=69.49){$GP = 2.50;}
if($mark>=59.50 && $mark<=64.49){$GP = 2;}
if($mark>=54.50 && $mark<=59.49){$GP = 1.50;}
if($mark>=49.50 && $mark<=54.49){$GP = 1;}
else { $GP = 0; }
echo $GP;
Expected output here is 3.67 but here I am getting 0. Can anyone explain why I am getting 0?
Each if is evaluated independently. Regardless of what happens with the first couple of ifs, you reach the last one, and since $mark isn't between 49.50 and 54.49, the code enters the else block, and sets $GP to 0. If you replace all the ifs except the first one with else ifs you'll get the intended logic:
$mark=80;
if($mark>=84.50 && $mark<=100){$GP = 4;}
else if($mark>=79.50 && $mark<=84.49){$GP = 3.67;}
else if($mark>=74.50 && $mark<=79.49){$GP = 3.33;}
else if($mark>=69.50 && $mark<=74.49){$GP = 3;}
else if($mark>=64.50 && $mark<=69.49){$GP = 2.50;}
else if($mark>=59.50 && $mark<=64.49){$GP = 2;}
else if($mark>=54.50 && $mark<=59.49){$GP = 1.50;}
else if($mark>=49.50 && $mark<=54.49){$GP = 1;}
else { $GP = 0; }
echo $GP;
Your code fails because your else only applies to the last condition. That means all the previous if statements have come to nothing.
You should use elseif to make sure that the conditions are truly distinct:
$mark=80;
if($mark>=84.50 $GP = 4;
elseif($mark>=79.50) $GP = 3.67;
elseif($mark>=74.50) $GP = 3.33;
elseif($mark>=69.50) $GP = 3;
elseif($mark>=64.50) $GP = 2.50;
elseif($mark>=59.50) $GP = 2;
elseif($mark>=54.50) $GP = 1.50;
elseif($mark>=49.50) $GP = 1;
else $GP = 0;
echo $GP;
As you see, you can also dispense with the && additional condition. Finally, you don’t need braces ({}) around a simple statement.
<?php
$mark=80;
if($mark>=84.50 && $mark<=100){$GP = 4;}
elseif(abs($mark>=79.50 && $mark<=84.49)){$GP = 3.67;}
elseif($mark>=74.50 && $mark<=79.49){$GP = 3.33;}
elseif($mark>=69.50 && $mark<=74.49){$GP = 3;}
elseif($mark>=64.50 && $mark<=69.49){$GP = 2.50;}
elseif($mark>=59.50 && $mark<=64.49){$GP = 2;}
elseif($mark>=54.50 && $mark<=59.49){$GP = 1.50;}
elseif($mark>=49.50 && $mark<=54.49){$GP = 1;}
else { $GP = 0; }
echo $GP;
?>
Your comparision not in proper way check above code.
I have the following code in a Laravel 5.4 Blade view:
#php($strVal = $character->Strength)
#php($strMod = 0)
<?php
if ($strVal == 10 || 11) {
$strMod = 0;
} elseif ($strVal == 12 || 13) {
$strMod = 1;
} elseif ($strVal == 14) {
$strMod = 2;
} else {
$strMod = 2;
}
?>
It takes data from a MySQL table.
$strVal is an int from the table. The code creates a var called $strMod and goes through a number of if/elseif statements to see what it will be equal to.
It's shown on a webpage as follows:
<div class="huge charMod">+{{$strMod}}</div>
My issue is that it displays as "+0" no matter what strVar equals. strVar is working fine, I can pull it from the DB and display it via {{ $strVal }} but strMod refuses to take a value other than 0.
$strVal == 10 || 11 will always return true
Because that's not how comparisons work in PHP. The == operator has a higher precedence than || operator, so it will be performed first.
It means that $strVal == 10 || 11 gets turned into false || 11 .. which is true.
Instead of that code, I would recommend:
$map = [
10 => 0,
11 => 0,
12 => 1,
13 => 1,
// you dont actually need 14, because default value is aready 2
];
$result = 2;
if (array_key_exists($strVal, $map)) {
$result = $map[$strVal];
}
$strVal = $result;
Or, if you are using PHP 7.0+ it all can actually be written as:
$map = [10 => 0, 11 => 0, 12 => 1, 13 => 1];
$strVal = $map[$strVal] ?? 2;
You need to change your if checks to
if ($strVal == 10 || $strVal == 11) {
$strMod = 0;
} elseif ($strVal == 12 || $strVal == 13) {
$strMod = 1;
} elseif ($strVal == 14) {
$strMod = 2;
} else {
$strMod = 2;
}
if ($strVal == 10 || 11) will always return true. If you want to check two values you need to specify that by using the variable == value again as in the example above.
I am working on a bit of PHP and I've come upon a bit of issues.
I am using PHP to randomly choose a number from 1-360. I am trying to compare the answer to a list of value determined by range.
$NumberA = rand(0,180);
$NumberB = rand(0,180);
$NumberC = $NumberA + $NumberB;
if ($NumberC = range(0,21) {
$result = "Orange";
}
elseif ($NumberC = range(22,42) {
$result = "Red";
}
elseif ($NumberC = range(43,63) {
$result = "Blue";
}
//This goes on for a while ...
else {
$result = "Green";
}
echo = $result;
Anytime i do this, the result always assigns the value of "Orange" to $result .
Im sure im doing something wrong here, please help!
First of all, you used just one '=' to compare while it should have been '=='. Second range() generates an array and you cannot compare an integer to an array. Third why generating the range every single time when you can check that $NumberC lies between the minimum and the maximum numbers of the range?
Change your code to:
$NumberA = rand(0,180);
$NumberB = rand(0,180);
$NumberC = $NumberA + $NumberB;
if ($NumberC >= 0 && $NumberC <= 21) {
$result = "Orange";
} elseif ($NumberC >= 22 && $NumberC <= 42) {
$result = "Red";
} elseif ($NumberC >= 43 && $NumberC <= 63) {
$result = "Blue";
} else {
$result = "Green";
}
echo $result;
Shall work. Hope this helps.
At the moment I've 3 conditions that need to be met randomly one time on 3 pages. So if page one gets randomly the second condition, page 2 can only get randomly condition 1 or 3 and so on. For that I write to a database (I already using a database to log activities) and call it on the other pages to see which condition already has been met on previous pages.
I created for the first page a random function:<?php $rand = rand(1, 3); ?>
The second page:
if ($row['manip_1'] == 1){
$man = rand(2,3);
} elseif ($row['manip_1'] == 2){ //should do randomly 1 or 3,
$man_a = rand(1,2); //but don't know how to skip #2 in a
if($man_a == 2){ //random function, so solved it like this
$man = 3;
} else {
$man = 1;
}
} else {
$man = rand(1,2);
}
The third page:
if ($row['manip_1'] == 1 && $row['manip_2'] == 2){
$man = 3;
} elseif ($row['manip_1'] == 1 && $row['manip_2'] == 3){
$man = 2;
} elseif ($row['manip_1'] == 2 && $row['manip_2'] == 1){
$man = 3;
} elseif ($row['manip_1'] == 2 && $row['manip_2'] == 3){
$man = 1;
} elseif ($row['manip_1'] == 3 && $row['manip_2'] == 1){
$man = 2;
} else {
$man = 1;
}
The problem now is that I suddenly need to have a fourth page. Meaning that there also should be a fourth condition. That also means that the number of possibilities are now 25 on the last page. I can program it again like I did before, but I was wondering if there is not any more convenient way to program a random condition that is dependent on the conditions of previous pages. During the 4 pages each condition can only be shown once.
Define an array of the conditions, and store them in the session, e.g.
$conditions = array(1,2,3,4);
$_SESSION['conditions'] = $conditions;
Randomization is easy:
array_shuffle($_SESSION['conditions']);
Then on each of the individual pages, you just pop off one of those values from the session:
<?php
# page 1
session_start();
$random_condition = array_pop($_SESSION['conditions']);
and do so for all the other pages:
<?php
#page n
session_start();
$random_condition = array_pop($_SESSION['conditions']);
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I set one variable to true based on other conditions. Here, instead of doing
if ($vara && $varb && $varc)
I'm something like below. Problem is, I'm just not getting something right. Can you please help me?
<?php
$onward = false;
$vara = 11;
$varb = 21;
$varc = 3;
if ($vara == 1)
{$onward = true;}else{$onward = false;}
if ($varb == 2)
{$onward = true;}else{$onward = false;}
if ($varc == 3)
{$onward = true;}else{$onward = false;}
if ($onward)
{
echo "Ok";
}else {echo "Not ok";}
?>
Each of your conditions ignores the result of the previous condition. You need to include the previous state of $onward in each subsequent test:
if ($vara == 1)
{$onward = true;}else{$onward = false;}
if ($varb == 2 && $onward)
{$onward = true;}else{$onward = false;}
if ($varc == 3 && $onward)
{$onward = true;}else{$onward = false;}
This way, varb and varc are only tested if $onward is still true after the previous test.
This is a particularly ugly way of writing code. If you have three large conditions and you don't simply want to join them on one line as in your $vara && $varb && $varc, you should be writing it this way:
$onward = ($vara == 1)
$onward = $onward && ($varb == 2);
$onward = $onward && ($varb == 3);
Any time you're simply returning/setting something to true/false in the branches if your if statement, you should just be returning/setting the condition itself.
That is, this:
if (condition) {
return true;
} else {
return false;
}
should always be written:
return condition;
The problem is, you are overwriting the value of $onward in every if-statement. Just use
$onward = $vara == 1 && $varb == 2 && $varc == 3;
I think you are looking for:
$onward = $vara == 1 || $varb == 2 || $varc ==3;
or
$onward = $vara == 1 && $varb == 2 && $varc == 3;
depending on your goal.
With this:
if ($varc == 3)
{$onward = true;}else{$onward = false;}
$onward will always be false if $varc is not 3.
I'm sure you mean
if (($vara == 1) && ($varb == 2) && ($varc == 3))
{$onward = true;}else{$onward = false;}
or even
$onward = (($vara == 1) && ($varb == 2) && ($varc == 3));
A better way of doing this:
if ($vara == 1) {$onward = true;} else {$onward = false;}
is this:
$onward = ($vara == 1);
You're overwriting the same variable each time, so the first two conditions are actually pointless.
I don't know why you'd want to do this over the first, more succinct approach, which will have the same logical effect:
if ($varc ==== 1 || $varc === 2 || $varc === 3)
$onward = true;
else
$onward = false;
Or even just:
$onward = $varc === 1 || $varc === 2 || $varc === 3;
Beware also of ever doing == 1. In comparisons, data are coerced to their truthy/falsy equivalents, so any truthy value will resolve to true in the comparison == 1. To test that something is literally 1, use ===. (This is a good rule generally unless you know you explicitly want to test for value and not type.)