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.
Related
Is there a way to get the true statement from $this if condition:
$a = 1;
$b = 3;
$c = 7;
if ($a == 3 || $b == 4 || $c == 7) {
echo "The true statement was: ";
}
I expect to get this output:
The true statement was: 7
Is it possible to do this in PHP?
Or better to say how can i check which statement has triggered the if condition?
You can't without multiple conditions. Whatever answer you will get here eg:
Inline if statements
Wrap in a function
Condition result assignment in the condition
Switch
Loops
etc. Will always require you to have multiple conditions.
If you don't mind multiple conditions and just looking for most elegant way to write it, thats another question and we can help.
This can only ever show 1 true statement because of how the if works:
$a = 1;
$b = 3;
$c = 7;
if (($t = $a) ==3 || ($t = $b) == 4 || ($t=$c) == 7) {
echo "The true statement was: $t";
}
What happens here is it sets $t to each variable and then checks if the assignment result (which is the value) was successful. Since this is an || then it stops at the first success and so $t will have the last compared value.
Try this.
<?php
$day = 1;
$month = 3;
$year = 2017;
$str = "The true statements are: " . ($day == 3 ? "$day, " : "") . ($month == 4 ? "$month, " : "") . ($year == 2017 ? "$year, " : "");
echo substr($str, 0, strlen($str) - 2);
?>
If I understand correctly, this should work.
The strlen($str) -2 is to remove the trailing ", ".
Here is a solution with boolean variables:
$day = 1;
$month = 3;
$year = 2017;
$cday = $day == 3;
$cmonth = $month == 4;
$cyear = $year == 2017;
if ($cday || $cmonth || $cyear) {
echo "The true statements are: ";
if($cday) echo "$day<br>\n";
if($cmonth) echo "$month<br>\n";
if($cyear) echo "$year<br>\n";
}
This might help -
// actual values
$day = 1;
$month = 3;
$year = 2017;
// values & variable names to check
$checks = array(
'day' => 1,
'month' => 4,
'year' => 2017,
);
// Loop through the checks
foreach($checks as $check => $value) {
// compare values
if($$check == $value) {
// output and stop looping
echo "The true statement was: $check -> $value";
break;
}
}
Demo
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 want to create 8 random integers, that are unique and in the range between 0 and 99.
Is there a neater solution, than the one that I thought of?
<?php
do {
$rnd1 = rand(0,99);
$rnd2 = rand(0,99);
$rnd3 = rand(0,99);
$rnd4 = rand(0,99);
$rnd5 = rand(0,99);
$rnd6 = rand(0,99);
} while (($rnd1 == $rnd2) ||
($rnd1 == $rnd3) ||
($rnd1 == $rnd4) ||
($rnd1 == $rnd5) ||
($rnd1 == $rnd6) ||
($rnd2 == $rnd3) ||
($rnd2 == $rnd4) ||
($rnd2 == $rnd5) ||
($rnd2 == $rnd6) ||
($rnd3 == $rnd4) ||
($rnd3 == $rnd5) ||
($rnd3 == $rnd6) ||
($rnd4 == $rnd5) ||
($rnd4 == $rnd6) ||
($rnd5 == $rnd6) );
?>
Try the code below.
It will create an array containing 8 random values between 0 and 99 while checking if they have been added already to ensure they are unique across.
$numbers = array();
while ( count($numbers) <= 8 ) {
$x = mt_rand(0,99);
if ( !in_array($x,$numbers) ) {
$numbers[] = $x;
}
}
print_r($numbers);
Also see these two Q&A's about using mt_rand vs. rand
Difference between mt_rand() and rand()
What's the disadvantage of mt_rand?
This is an optimized version that relies on the array indexes. The complexity of this solution is linear rather than n2 as in #AlexAndrei's answer. It relies on using array indexes as the guarantee there are no duplicates so there is no need to add the overhead of in_array.
$random_numbers = [];
do {
$rand = rand(0, 99);
$random_numbers[$rand] = $rand;
} while( count($random_numbers) < 9 );
$random_numbers = array_values($random_numbers); // This strips the keys
print_r($random_numbers);
If you are using a foreach you can even drop the array_values call which would also increase the speed of your code, but the numbers in the array will be in the form: [ a => a, b => b, ... ]
$rnds = [];
do {
$newRnd = rand(0, 99);
$rnds[] = $newRnd;
} while(in_array($newRnd, $rnds) && count($rnds) < 9);
print_r($rnds);
This question already has answers here:
display data in multiple columns
(3 answers)
Closed 8 years ago.
I am trying to create multi-list of records with 4 records per list without knowing how many records there are. However, I cannot figure out how to handle the math. I manually typed in $n == 5 || $n == 9 etc knowing it is stipud and cannot exactly solve the problem. Can anyone help me how to handle that. Also, the lists underneath works well only if the total number of records cannot be evenly divided by 4. If it can, it will create a blank list at the end.
$query = "SELECT * FROM `table` WHERE `field` = $whatever";
if ($result = $con->query($query)){
$n = 1
$row_cnt = $result->num_rows;
$total_lists = round($row_cnt / 4, 0);
$current_list = 1;
echo "<ul>List $current_list of $total_lists";
while ($row = $result->fetch_assoc()) {
echo "<li>$row['something']</li>";
if ($n == 5 || $n == 9 || $n == 13 || $n == 17 || $n == 21 || $n ==25 || $n ==29 || $n == 33 || $n == 37 || $n == 41 || $n == 45 || $n == 49 || $n == 53 || $n == 57 || $n == 61 || $n == 65 || $n == 69 || $n == 73 || $n == 77){
echo "</ul>";
$current_list = $current_list + 1;
echo "<ul>List $current_list of $total_lists";
}
$n = $n + 1;
}
echo "</ul>";
}
Thanks in advance for the help. :)
SOLVED:
$query = "SELECT * FROM `table` WHERE `field` = $whatever";
if ($result = $con->query($query)){
$n = 0
$row_cnt = $result->num_rows;
$total_lists = ceil($row_cnt / 4);
$current_list = 1;
echo "<ul>List $current_list of $total_lists";
while ($row = $result->fetch_assoc()) {
$n++;
echo "<li>$row['something']</li>";
if ($row_cnt > 4) {
if ($n % 4 === 0) {
echo "</ul>";
$current_list = $current_list + 1;
echo "<ul>List $current_list of $total_lists";
}
}
}
echo "</ul>";
}
You're actually not too far off track from what you might want to be doing, you just need to use one additional tool to accomplish it: the % or modulus operator.
The modulus operator will return the remainder of division problem:
$x = 5 % 2; // 1
Looking at your logic, your action needs to be taken when your incrementer ($n) minus 1 divided by 4 would have a remainder of 0:
if (($n - 1) % 4 === 0)
{
//your <ul> insertion could go here.
}
Here's a link to the PHP manual page discussing mathematical operators:
http://us1.php.net/manual/en/language.operators.arithmetic.php
Alternatively, you could use a simple array_chunk() on your db results. Provide the proper grouping (in this case by four's) to it and them loop them thru foreach. Consider this example:
// dummy data (values from db)
$values_from_db = array(
array('id' => 1, 'something' => 'list1'),
array('id' => 2, 'something' => 'list2'),
array('id' => 3, 'something' => 'list3'),
array('id' => 4, 'something' => 'list4'),
array('id' => 5, 'something' => 'list5'),
array('id' => 6, 'something' => 'list6'),
array('id' => 7, 'something' => 'list7'),
array('id' => 8, 'something' => 'list8'),
array('id' => 9, 'something' => 'list9'),
);
$values_from_db = array_chunk($values_from_db, 4); // group them by four's
?>
<?php foreach($values_from_db as $key => $value): ?>
<ul style="list-style-type: none;">
<?php foreach($value as $index => $element): ?>
<li><?php echo $element['id'].'.'.$element['something']; ?></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
I want to have an if statement in PHP to calculate if an INT equals only certain numbers. The pattern would go like: 3, 5, 8, 10, 13, 15, 18, 20 etc. It adds 3, then 2, repeating. The pattern is consistent with the last digit (3,5,8,0), but I don't want to include the first 0.
I could do it this way, but it could go on forever...
if($int == 3 || $int == 5 || $int == 8 || $int == 10 ....)
{
//do stuff
}
This way also does multiples of 3...
if ($int % 3 == 0)
{
//do stuff
}
But doesn't do the pattern I want. What's the right way of doing this?
You can do it like this.
Get the remainder of the division by 10 and then check for 0, 3, 5 or 8.
if ($int > 0) {
$int = $int % 10;
if ($int == 0 || $int == 3 || $int == 5 || $int == 8)
{
//do stuff
}
}
function check($t) {
$mod = $t % 10;
if ($mod == 0 || $mod == 3 || $mod == 5 || $mod == 8)
echo $t;
}
$arr = array(3, 6, 8, 10, 20, 13, 15, 19);
array_map('check', $arr);