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.
Related
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.
So I'm busy on a verification system, when the player of the game puts a sentence in his/her status, in this case: HT-N5I4-S3ZI-GU6A and my script checks if it's the same one as the code in my db. If it is, its proven to be their account. Because I've experienced that people make accounts under fake names.
When I echo the code in my db, it echoes:
HT-N5I4-S3ZI-GU6A
And the one in my test players mission echoes:
HT-N5I4-S3ZI-GU6A
It is exactly the same code, but when I wanna compare them like this:
if($_SESSION['user']['email_activation'] == $functions->getMotto($_SESSION['user']['username']))
{
echo "Works";
}
else
{
echo "Doesnt work";
}
It aways echoes 'doesn\'t work'. Did I use the wrong equalization operator? Or any other mistakes?
Thanks
Try This Code
if($_SESSION['user']['email_activation'] === $functions->getMotto($_SESSION['user']['username']))
{
echo "Works";
}
else
{
echo "Doesnt work";
}
Thanks.
Currently my code looks like this
<?php
if($controlSomething) {
include(header);
echo "something";
include(footer);
die();
}
if($anotherControl) {
include(header);
doSomeOperations(); // would throw an error if $controlSometing is true
echo "something else";
include(footer);
die();
}
?>
Clearly this is not a good way to do things. I want to move to something cleaner.
I created a view class to wrap the page and avoid the include(header)/include(footer) repetition. I also converted all the conditional statement, changing if($condition) into function condition ().
Now what should I do ? I was thinking of something like this :
if(condition1()) { message for condition1 ...}
else if(condition2()) {}
The problem is that this file might be included by another file that will be executed because there is no die statement anymore.
So should I include a var like "Die" in the view object and check its value before doing anything on every page ? This doesn't seem clean to me.
I have thousands of lines of code like this so the more simple/automated the solution, the better.
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  ';
}
elseif($sampleResult != 0 && $this->CheckAnswer($questionId,$answerResult['id'],$sampleResult))
{
echo '<input type="radio" name="question['.$questionId.']" value="'.$answerResult['id'].'" checked/>'.$answerResult['name'].'hee  ';
}
else
{
echo '<input type="radio" name="question['.$questionId.']" value="'.$answerResult['id'].'" />'.$answerResult['name'].'  ';
}
}
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?
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.