While statement repeats infinitely - php

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.

Related

Store time in a session

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.

Only one line executed in a false IF - PHP

I'm integrating pagination to a wordpress block, what should happen is whenever i click "older posts" button, the post offset is incremented in another php file (the block file and the increment files are separate files).
Whenever I click the older posts however some magic happens that shouldn't:
The problem is with one of the if statements, what happens is that IF statement evaluates to false, yet that one line that I marked in the code still gets executed (the echo doesn't execute but the $_SESSION["inf"] = 0; does).
Here is the code.
if(isset($_POST["paginate_btn"])){
if(isset($_SESSION["inf"])){
echo "PREVIOUS:".(string)($_SESSION["inf"]);
$_SESSION["inf"] = $_SESSION["inf"] + 5;
echo "<br/> incremented by 5, RESULT:".(string)($_SESSION["inf"]);
}
else{
echo "<br/> isset session inf failed, set to 5";
$_SESSION["inf"] = 5;
}
}
else if(isset($a) and ($a != $url)){
echo "<br/>dif page";
if(isset($_SESSION["inf"])){
echo "<br/> unset";
$_SESSION["inf"] = 0; <----- THIS LINE HERE!
}
else{
echo "<br/>session inf not set and button not pressed";
}
}
else{
echo "<br/> button not pressed, a is not set and is same page";
$_SESSION["inf"] = 0;
}
I've messed around with this for hours and hours, I don't understand what the case is here..
var_dump((isset($a) and ($a != $url)));evaluates false, yet still only that one line of code there gets executed, the echo doesn't, not even the dif page echo before it....
And the funny part, if I comment out that one line, this no longer happens, I've even tried setting it to different variables there so it definitely gets run solo somehow.
Var_dump code after the if statements.
echo "<br/>--1---<br/>";
var_dump((isset($a) and ($a != $url)));
echo "<br/>--2---<br/>";
var_dump((isset($_SESSION["inf"])));
echo "<br/>---<br/>";
echo $_SESSION["inf"];
echo "<br/>---<br/><br/>";
Another part of the code sets $_SESSION["inf"] to 0.
If you don't see the echos, they don't get executed.

Using if else statement based off of $_POST value to echo message

Pretty sure this is a quick and easy question but I have a form that on action POST goes to a confirmation page. I need a message to display on the confirmation page if the user selects county1 but if user selects county2, county3, or county4. However, when I setup the statement it's not working. Probably a syntax error or two on my part. Any help would be greatly appreciated.
A messy idea of what I think should work:
<?php $county=$_POST['County'];
if ($county="Polk") {
echo "Important message about your county"; }
else {
echo " "; // Or nothing at all
}
?>
But
<?php echo $_POST['County'] ?>
displays the name of the county so I know the submission is carrying through. Thoughts on why my above code wouldn't be working? If you could flag syntax errors or code placement that'd be much appreciated! Thank you!
Inside the if condition you should use two equal operators instead of one . try this code
<?php
$county = isset($_POST['County'])?$_POST['County']:"";
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
?>
Use double equal in your if statement for comparison
See the answer and read the comment to understand why you have to change it
if ($county="Polk") {
Single equal is assignment operator, it assign value
Change this line to this
if ($county=="Polk") {
So your whole code should look like this
$county=$_POST['County'];
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
Use double equal sign, double equal is comparison operator,
Here you are checking if the $county is equal to Polk or not,
Means you are comparing value

A better way to check if a user were logged in and generate like dislike link

I have a script that takes some 500 rows from a table, and based on whether or not a user is logged in, generates a link to like or dislike the item.
The way it currently goes is like this:
//Select * from table;
//while(){
if($userLogged)
{
echo $row['columnName'].' Like - Dislike';
}else{
echo $row['columnName'];
}
}
This way, it checks if a user is logged at each and every row. $userLogged is set in a file that's included on this page.
What would be a better way to do this instead of checking if a user were logged in inside the loop for each and every row?
Not getting your question quiet well but I think you want to prevent condition each time you loop so you can check the condition first and than loop accordingly, example
if($userLogged) {
while(condition) {
}
} else {
while(condition) {
}
}
This way you don't have to check the condition inside the loop each time it loops
Use sessions here
session_start();
if(isset($_SESSION['userLogged']))
{
echo $row['columnName'].' Like - Dislike';
}else{
echo $row['columnName'];
}
If the code for the like and dislike is identical for each entry then you could do something like this.
$links = ($userLogged)?"Like - Dislike":"";
while (condition){
echo $row['columnName'].$links;
}

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