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.
Related
I have a code that works just fine but I have a few questions about it. I don't understand the logic of something. The code is:
<?php
session_start();
if(!isset($_SESSION['t0']))
{
$_SESSION['t0']=time();
echo $_SESSION['t0']."if<br />"; //why this is never printed?
}
else
{
if(time()>=($_SESSION['t0']+3))
{
echo $_SESSION['t0']."else-ul";
$culoare="rgb(".rand(0,255).",".rand(0,255).",".rand(0,255).")";
$_SESSION['t0']=time();
}
}
?>
The questions would be:
1. Why the first echo is never printed?
2. Why (time()>=($_SESSION['t0']+3)) isn't always true since $_SESSION['t0'] is updated every second because of session[t0]=time() ?
Thank you!
First echo statement does get executed, but it happens only on very first time. Once you had your session started value for $_SESSION['t0'] is always set, so the if condition will always return false.
time()>=($_SESSION['t0']+3) condition is true when 3 seconds has passed after execution of the code. So if you reload your page after 2 seconds it will not get executed.
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
?>
Basically I have this code. It shows on header_new when the postId of the wordpress page is part of the array.
Now I want a 3rd option, a new header with id . So it shows up when the its part of a new set of array. I tried else if it didn't work tho.
Can you guys help me add a new php line to do just that. Basically a 3rd else if statement to execute a new header given if it is part of an array.
Please be detailed since I'm a noob at PHP
<div id="wrapper" class="<?php echo $post->ID;?>">
<?php $pagesArray = array(7686,7913,7915,8019,8131,8180,8676,8769,8796,8810,8812,8814,8818,8815); ?>`
<?php if(in_array($post->ID, $pagesArray)){ ?>
<div id="header_new"><!--header!-->
</div><!--header!-->
<?php } else {?>
<div id="header-out">
</div>
You cannot order your code like this:
if (something){
xxxxx
} else {
xyxyx
} else if (something else) {
zzzzz
}
because your loop ends with else. else if must come before else case, because else
finishes the loop. The third else if will never be fulfilled.
Edit:
When you put your second else if, you need to specify your test.
it is else if(test clause){some execution}.
Here is a link, this may help you out: Php else if
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.
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.