if elseif statement in PHP - php

Both if and elseif statement are working at the same time.
while($answerResult = mysql_fetch_assoc($answers))
{
if($reportResult != 0 && $this->CheckAnswer($questionId,$answerResult['id'],$reportResult))
{
echo '<input type="radio" name="question['.$questionId.']" value="'.$answerResult['id'].'" checked/>'.$answerResult['name'].'aaa &nbsp';
}
elseif($sampleResult != 0 && $this->CheckAnswer($questionId,$answerResult['id'],$sampleResult))
{
echo '<input type="radio" name="question['.$questionId.']" value="'.$answerResult['id'].'" checked/>'.$answerResult['name'].'hee &nbsp';
}
else
{
echo '<input type="radio" name="question['.$questionId.']" value="'.$answerResult['id'].'" />'.$answerResult['name'].' &nbsp';
}
}
I'm trying to do if my first statement is true then don't look other conditions but if my first condition is false then check elseif condition.
However, my elseif condition is working even though first if condition is true.
I put 'hee' and 'aaa' at the end of the 'echos' to see which one is printing.
The result:
First, if statement is working and printing with 'aaa' then looking at the elseif statement and checked this and print 'hee'.
So, I don't know what i am missing and can't find any solution, Is there any suggestion?

The if, elseif, and else in one statement are exclusive, so this shouldn't happen.
The whole statement is in a while loop ... are you sure it's not the if being executed in the first iteration and the elseif being executed in the second iteration?

Related

Stop processing PHP file completely

I need to be able to completely stop processing a file/page if a certain condition exists, for examaple:
<?php
if ($conditions == "true") {
echo "Condition is true";
}
else {
echo "Condition is not true";
//Command here to completely stop processing all the page;
}
?>
<?php
//nothing else should run here if the aboe condition is not true
?>
<?php
//or this part
?>
So basically, if the condition in the first if else is false, no further processing is done, including ignoring the following two parts.
Can this be done?
If confused with die() and exit as from what I have read, it will only stop the first part but continue to run the second and third parts within the
Thank you.

skipping if statement if condition met and continue executing the code below php

I will try to explain what I want to achieve with commented code.
what I am trying to do is skip through if statement if the condition is met and continue executing the code outside conditional statement.
<?php
if (i>4) {
//if this condition met skip other if statements and move on
}
if (i>7) {
//skip this
?>
<?php
move here and execute the code
?>
I know about the break, continue, end and return statement but that is not working in my case.
I hope this clears my question.
If your first condition met and you want to skip other condition you can use any flag variable as below :
<?php
$flag=0;
if (i>4)
{
$flag=1;
//if this condition met skip other if statements and move on
}
if (i>7 && flag==0)
{
//skip this
?>
<?php
move here and execute the code
?>
You can use a goto
<?php
if (i>4)
{
//if this condition met skip other if statements and move on
goto bottom;
}
if (i>7)
{
//skip this
?>
<?php
bottom:
// move here and execute the code
// }
?>
But then again, lookout for dinosaurs.
Use if-elseif-else:
if( $i > 4 ) {
// If this condition is met, this code will be executed,
// but any other else/elseif blocks will not.
} elseif( $i > 7 ) {
// If the first condition is true, this one will be skipped.
// If the first condition is false but this one is true,
// then this code will be executed.
} else {
// This will be executed if none of the conditions are true.
}
Structurally, this should be what you're looking for. Try to avoid anything that will lead to spaghetti code like goto, break, or continue.
On a side note, your conditions don't really make much sense. If $i is not greater than 4, it will never be greater than 7, so the second block would never be executed.
I typically set some sort of marker, such as:
<?php
if (i>4)
{
//if this condition met skip other if statements and move on
$skip=1;
}
if (i>7 && !$skip)
{
//skip this
?>
<?php
move here and execute the code
?>
<?php
while(true)
{
if (i>4)
{
//if this condition met skip other if statements and move on
break;
}
if (i>7)
{
//this will be skipped
}
}
?>
<?php
move here and execute the code
?>

PHP - Creating Multiple Conditional Statements For a Single Element

I'm still new in PHP and I need to know how I can create two or more conditional statements for a single elements. For instance, maybe if I had a form that I wanted to perform a certain action if some conditions were true.
I understand that one of the way is to group the statements like:
if(arg1 && arg2 && arg3) {
echo "All of these statements are true";
}
How can I separate this condition but making sure that it executes at the same time?
You can use nested if statements to accomplish the same check.
if(arg1)
{
if(arg2)
{
if(arg3)
{
echo "All of these statements are true";
}
}
}
Try this.
if(($something == 1 ) && ($someotherthing !=7) && ($something == 2)) {
echo "All of these statements are true";
}
Changed answer after what I understood of you clarification:
If you want to know if the posted form has all the required fields in PHP then you could write :
if(isset($_POST['field_name']) && $_POST['field_name'] != "")
{
//The field is set and not empty.
}
else
{
echo "Field not set...";
}
w3schools tutorial on forms :
http://www.w3schools.com/php/php_forms.asp
As for the numeric part of your question take a look at regex :
http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial
EDIT 2 : As someone mentioned if you want to know if the field is set BEFORE being sent to the server, you will need to use Javascript.

While statement repeats infinitely

I have a website where i need to use a while statement, but when i use it, it repeats the echo infinitely. Although it looks like i could make it work without while, that isnt so, this is a simplified version of a final product that will need while.
<?php
$passlevel = '0';
while ($passlevel == '0')
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#800080")
{
echo "you have passed step one.";
$passlevel == '1';
}
else
{
echo "you didn't select purple.";
}
}
else echo "contact webmaster";
}
?>
Why is it echoing either contact webmaster or you didnt select purple an infinite number of times?
First, you probably need to change:
$passlevel == '1';
to
$passlevel = '1';
The first is a comparison equals, not an assignment equals.
Second, if $color is not #800080, then the loop does not terminate and thus repeats forever as nothing in the loop causes the value to change.
I'm not entirely sure of the point of this loop in the first place. It should work perfectly fine without the loop, however you've stated that your code is a simplified version of something more complicated that indeed needs a loop. Perhaps you can elaborate.
You're not providing any way out of the loop. If $_GET['box_1_color'] isn't purple the first time through the loop, it can't possibly become anything else the second time through the loop, so it'll keep being the wrong color each and every time.
I'm not certain what you intended for this loop to accomplish. If you're trying to have the user enter a new value each time, you won't be able to do that with a loop in PHP. You'll have to regenerate the entire page (with an error message, presumably) and ask the visitor to submit the form again.
In the case of "contact webmaster", you need to break out of the loop, either with the break expression or by setting your $passlevel to anything other than zero. A more serious real problem is revealed in #Mike Christensen's answer, though
If $_GET['box_1_color'] is not set, the variable $passlevel will never be changed.
<?php
$passlevel = 0;
while ($passlevel == 0 || $passlevel == 2)
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#800080")
{
echo "you have passed step one.";
$passlevel = 1;
}
else
{
echo "you didn't select purple.".'try again.';
}
}
else
{
echo "contact webmaster";
$passlevel = 2;
}
}
?>
You need to define another passlevel for failure, to stop the while loop. Also, don't put any quotes around integers.

programming 101 if/else statement

This is probably the easiest question to answer that you will find on stackoverflow, but I would like to get this confusion out of my head once and for all. Consider the following if statement:
if(x > 0)
{
echo 'Inside if';
}
// apparently there is a hidden else here....
echo 'This comes after if';
And now consider the following one:
if(x > 0)
{
echo 'Inside if';
}
else
{
echo 'Inside else';
}
echo 'This comes after if/else';
In the first example, if the condition evaluates to true, "Inside if" will be printed, but won't what comes after the if ("This comes after if") get printed also? I mean, I don't have return inside my if, so the code should continue normally, right?. Same thing for the second if statement, whatever comes after the statement will get printed because the execution of the code will continue normally. Is there really a virtual else after an if-statement if we don't explicitly define one? I mean, if what comes after my if statement is printed whether the condition evaluates to true or not, then there's not really a virtual else after my if. Also, When is an Else absolutely necessary in an if-then-else statement instead of just relying on the "virtual else" as in the first example? Please shed some light on this.
Thank you
An else is "absolutely necessary" whenever you want to actually do something if the if condition evaluated to false. If you only want to do something in the case where it's true, and absolutely nothing when it's false, you can skip the else part.
There isn't really a hidden else. A conditional statement is a way to branch off the procedural execution of your code temporarily. Once completed, it will continue where it left off unless you do a return from within a function for example.
Simple example of where you need an ELSE:
IF (loadfile == True)
{
println("file loaded...on to processing...");
}
ELSE
{
:: raise an error and stop execution ::
}
:: continue with processing file ::
The difference between the "virtual else" and the else is that the virtual else is always executed, whereas the real else is only conditionally executed. For example, consider that this:
if(x > 0)
{
echo 'Inside if';
}
else
{
echo 'Inside else';
}
echo 'This comes after if/else';
is exactly the same as this:
if(x > 0)
{
echo 'Inside if';
}
if(x <= 0)
{
echo 'Inside else';
}
if(x == x)
{
echo 'This comes after if/else';
}
Your "virtual else" is not really an else at all, it is always executed.

Categories