I have a function where, if the user enters the correct answer, the score should increment. The validation function is working fine, but the score is not incrementing. Initially, I defined it as 0 and then it should increment based on the answer, but it's not working.
$score =0;
function checkscore ($n, $ans)
{
global $score;
$arr = array('a', 'd');
if (($n == 1) && ($ans == (count(array_intersect($arr, $_POST['a'])) == count($arr))))
{
$score++;
}
if (($n == 2) && ($ans == ($_POST['b'] == 'a')))
{
$score++;
}
if (($n == 3) && ($ans == "div[id='serenade']"))
{
$score++;
}
if (($n == 4) && ($ans != (($_POST['d1'] == 'B') && ($_POST['d2'] == 'C') && ($_POST['d3'] == 'A'))))
{
$score++;
}
return $score;
}
This is the function. Someone please help me.
This function will return 1 if $n is 3 or 4, 0 otherwise. The first two conditions will always be false, while the third will be true when $n==3 and the fourth when $n==4.
So it does increment occasionally, but only up to 1...
Related
I am familiar with using if else statement but my problem is how to display either if the condition is met.
if ($a == 1) {
echo 'B' OR 'C'; // just for reference
}
I have finally figure this out using nested loop
if ($a == 1) {
choiceresult = mt_rand(1,2)
if (choiceresult == 1) {
echo 'B';
}
if ( choiceresult == 2) {
echo 'C';
}
}
you have to fix your condition first :
if($a == 1)
Instead pf
if($a = 1)
I have written a PHP function to add the appropriate stems to each rank. I.E. 1st 2nd 3rd... so on and so forth.
When $num = 0 the displayed result is "0th", is there a way to display this a 'No Data' instead?
function ordinalSuffix($num) {
$suffixes = array("st", "nd", "rd");
$lastDigit = $num % 10;
if(($num < 20 && $num > 9) || $lastDigit == 0 || $lastDigit > 3) return "th";
return $suffixes[$lastDigit - 1];
}
Like this?
function ordinalSuffix($num) {
//Check if $num is equal to 0
if($num == 0){
//return
return 'No Data';
}
$suffixes = array("st", "nd", "rd");
$lastDigit = $num % 10;
if(($num < 20 && $num > 9) || $lastDigit == 0 || $lastDigit > 3) return "th";
return $suffixes[$lastDigit - 1];
}
function ordinalSuffix($num) {
$suffixes = array("st", "nd", "rd");
$lastDigit = $num % 10;
if(($num < 20 && $num > 9) || $lastDigit == 0 || $lastDigit > 3)
{
return "th";
}
elseif($num == "0")
{
return "no data";
}
return $suffixes[$lastDigit - 1];
}
This does not add to your function but I thought it would be fun to post a one-line solution anyways.
function ordinalSuffix($n) {
return ($n==0?'No Data':date('S',mktime(0,0,0,1,($n%10==0?9:($n%100>20?$n%10:$n%100)),2000)));
}
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 :)
I am stuck on some php coding, that seemed initially easy to accomplish. Here is what i would like to do :
<?php
$amountOfDigits = 1;
$numbers = range(1,3);
shuffle($numbers);
for($i = 0;$i < $amountOfDigits;$i++)
$digits .= $numbers[$i];
while ( have_posts() ) : the_post();
static $count = 0;
if ($digits == '1') {
//Do this if statement
if ($count == "2") }
elseif ($digits == '2') {
//Do this if statement
if ($count == "2" || $count == "3") }
elseif ($digits == '3') {
//Do this if statement
if ($count == "2" || $count == "3" || $count == "4") }
{ //here the rest of the code
?>
So depending on the $digits variable, the if statement is formatted to be used on the line above //the rest of the code
How to put this in PHP properly?
Thanks.
Robbert
If I understand your question properly, you want something like this:
if ($digits == '1')
$mycond = ($count == "2");
elseif ($digits == '2')
$mycond = ($count == "2" || $count == "3")
elseif ($digits == '3')
$mycond = ($count == "2" || $count == "3" || $count == "4")
then you can further use
if($mycond){
// blahblahblah
}
Well if you want the same execution block on each case, there is a very simple solution for that.
You should simply use a function that check status of "count" depending on "digits".
<?php
function checkCountAgainstDigits($count, $digits){
switch($digits){
case 1:
return $count === 1;
case 2:
return $count === 2 || $count === 3;
case 3:
return $count === 2 || $count === 3 || $count === 4;
default:
// The default case "ELSE"
return FALSE;
}
}
if(checkCountAgainstDigits($count, $digits)){
// do
}
?>
If you want a different one, your solution is correct.
if i have statement:
$a = 1;
$b = 2;
$c = 3;
if($a == 1 && $b == 2 && $c == 3)
{
echo 'correct';
}
else
{
echo 'what variable's weren't matched';
}
Is there any way of knowing what didn't watch instead of writing everything separately?
Cheers!
No. Your expression was turned into a boolean, so apart from checking the equality(s) again you cannot find out which triggered the "false".
You need to test each individually, but you could do something like this:
$a = 1;
$b = 2;
$c = 3;
$a_matched = $a == 1;
$b_matched = $b == 1;
$c_matched = $c == 1;
if($a_matched && $b_matched && $c_matched)
{
echo 'correct';
}
else
{
if (!$a_matched) echo 'a did not match!';
if (!$b_matched) echo 'b did not match!';
if (!$c_matched) echo 'c did not match!';
}
but that's less clear than just:
$a = 1;
$b = 2;
$c = 3;
if($a == 1 && $b == 2 && $c == 3)
{
echo 'correct';
}
else
{
if (!$a == 1) echo 'a did not match!';
if (!$b == 2) echo 'c did not match!';
if (!$c == 3) echo 'b did not match!';
}
Actually, heh, I take back my comment. You can rely on the boolean short-circuiting to set a variable indicating the last part of the conditional which was true:
if (($x = 'a') && $a == 1 && ($x = 'b') && $b == 2 && ($x = 'c') && $c == 3) {
echo "correct\n";
} else {
echo "$x is wrong\n";
}
Note, I would never write this in production code because it's goofy and very hard to understand what's supposed to be going on. But fun to fiddle with, at least.
Nope! That's not possible. You can make life a lot simpler by using arrays, though:
$results = array(1, 2, 4);
$expected = array(1, 2, 3);
$count = count($results);
$wrong = array();
for($i = 0; $i < $count; $i++) {
if($results[$i] !== $expected[$i]) {
$wrong[] = $i;
}
}
if(count($wrong) > 0) {
echo "There were wrong ones. They were at positions: " . implode(', ', $wrong);
} else {
echo "All good!";
}
For example.