How to execute php if condition once time only and didn't check it again, i put if condition in for loop and i want to check it one time only:
<?php
for($x=0;$x<3;$x++){
if($x == $x){
echo "";
}
else{
echo "hidden";
}
}
I need to execute first if one time only in for loop
I need to execute first if one time only in for loop. Well that's not how you do it, you need to compare with a variable and upon success just use break to come out of the loop. Something like:
<?php
$y = 0;
for($x=0; $x<3; $x++){
if($x === $y){
echo "X is Equal to Y";
break;
}
else {echo "hidden";}
}
echo "<br>". "You're outside the loop!";
?>
Set a flag, if flag is true don't execute if condition.
<?php
$flag = 0;
for($x=0;$x<3;$x++) {
if($x == $x && $flag == 0){
echo "";
$flag = 1;
}
else{
echo "hidden";
}
}
Related
I have a databse that has food reviews on it, and I want to show if a food is vegetarian or not.
I want to add a echo "No"; but I don't know how.
I've tried using else, but it doesn't show. I've also tried <?php for($x=1;
Here is my code:
<p>Vegetarian: <span class="sub_heading"><?php for($x=0; $x < $find_rs['Vegetarian']; $x++)
{
echo "Yes";
}
; ?></span></p>
I want it to show no if it is 0 and yes if it is 1
<p>
Vegetarian:
<span class="sub_heading"><?= $find_rs['Vegetarian'] ? "Yes" : "No"?></span>
</p>
Simply add an if conditional inside of your loop that checks against the value of $x:
for ($x=0; $x < $find_rs['Vegetarian']; $x++) {
if ($x == 1) {
echo "Yes";
}
else if ($x == 0) {
echo "No";
}
else {
echo "x wasn't either 0 or 1 -- x was $x";
}
}
Note that the final else condition will output the value of $x due to the use of double-quotes.
if the condition only 2, just using if else statement,
<?php
for($x=0; $x < $find_rs['Vegetarian']; $x++){
if($x == 1){echo 'Yes';}else{echo 'No';}
}
?>
I am currently trying to learn PHP and am stuck on a do/while with else/if loop and am totally baffled. Would appreciate if anybody could let me know where i am going wrong with this.
<?php
$rollCount = 0;
$sixCount = false;
do {
echo "<p>So you want to roll a 6 ? </p>";
} while($sixCount == false);
$roll = rand(1,6);
$rollCount ++;
if($roll != 6) {
$sixCount = false;
echo "<p>You just rolled a " .$roll. " .</p>";
}
else {
$sixCount = true;
echo "<p>Nice, you just rolled a 6</p>";
}
echo "<p>It took {$rollCount} rolls to land 6... What are the odds!</p>";
?>
I think i may have written an infinite loop, as the preview screen i am working on just shows loading animation.
My little mind tries to look at it like this.
While $sixCount is equal to false, $roll a random number between 1 and 6 and add a +1 to roll count.
IF the $roll does not = to 6, than the $sixCount is false and print "you just rolled a $roll" or else $sixCount is true and you just rolled a 6.
I try to simplify it in my mind by putting into a sentence and transferring it into PHP. Is this the wrong way to look at PHP ?
Appreciate all help on this. Thanks!!
I think you're misunderstanding the concept of the do...while loop. You need to put all your conditions and output into the do part, and the while part simply defines the condition that should allow the do set to keep looping.
Try shifting your code under the while line into the do statement:
$rollCount = 0;
$sixCount = false;
echo "<p>So you want to roll a 6 ? </p>";
do {
$roll = rand(1,6);
$rollCount++;
if($roll != 6) {
$sixCount = false; // this line is unnecessary
echo "<p>You just rolled a {$roll}.</p>";
}
else {
$sixCount = true;
echo "<p>Nice, you just rolled a 6</p>";
}
} while($sixCount == false);
echo "<p>It took {$rollCount} rolls to land 6... What are the odds!</p>";
An alternative, using the while construct without do would possibly be more applicable to the way you've laid out your code:
$rollCount = 0;
$sixCount = false;
echo "<p>So you want to roll a 6 ? </p>";
while ($sixCount == false) {
$roll = rand(1,6);
$rollCount++;
if($roll != 6) {
$sixCount = false; // this line is unnecessary
echo "<p>You just rolled a {$roll}.</p>";
}
else {
$sixCount = true;
echo "<p>Nice, you just rolled a 6</p>";
}
}
echo "<p>It took {$rollCount} rolls to land 6... What are the odds!</p>";
It appears that you are writing the code you want to continuously execute until it rolls a 6 is outside of the while loop, this is what the code should look like.
<?php
$rollCount = 0;
$sixCount = false;
echo "<p>So you want to roll a 6 ? </p>";
do{
$roll = rand(1,6);
$rollCount ++;
if($roll != 6) {
$sixCount = false;
echo "<p>You just rolled a " .$roll. " .</p>";
}
else {
$sixCount = true;
}
}while($sixCount == false);
echo "<p>Nice, you just rolled a 6</p>";
echo "<p>It took {$rollCount} rolls to land 6... What are the odds!</p>";
?>
<?php
$division=$row['mark'];
$pass="Passed";
if($division>=80 && $pass==include "result.php")// Result.php has two value: one is `Pass` and the other is `Fail`.
{
echo "Letter";
}
elseif($division>=70 && $pass==include "result.php")
{
echo "First";
}
else
{
echo "Fail";
}
?>
What I want to output here is: if $division is equal to 80 and at the same time if $pass is equal to Passed, echo Letter. But if $division is less than 70, echo Fail; also $pass here equals to fail which is taken from result.php. I have been trying to output it with following code, but it does not work. It outputs FailFailFailFail when $division is less than 70.
Code for Result.php
<?php
$eng=40;
$mizo=40;
$hindi=40;
$maths=40;
$ss=40;
$science=40;
if ($eng>=40 && $mizo>=40 && $hindi>=40 && $maths>=40 && $ss>=40 && $science>=40)
{
echo "<font color=green>Passed</font>";
}
else
{
echo "<font color=red>Failed</font>";
}
?>
You are going about this all the wrong way. You can't compare the results of an include like that, not to mention they don't match properly anyway as you're comparing a single word string against a string with a whole bunch of HTML in it.
A better way to do it is to include results.php and store your answer in a variable. I have written an example below.
First off you need to change result.php to:
<?php
$eng=40;
$mizo=40;
$hindi=40;
$maths=40;
$ss=40;
$science=40;
if ($eng>=40 && $mizo>=40 && $hindi>=40 && $maths>=40 && $ss>=40 && $science>=40)
{
$test = "Passed";
}
else
{
$test = "Failed";
}
?>
Then you put the following in the first file:
<?php
$division=$row['mark'];
$pass="Passed";
include("result.php");// Result.php has two value: one is `Pass` and the other is `Fail`, store in $test.
if($division>=80 && $pass==$test)
{
echo "Letter";
}
elseif($division>=70 && $pass==$test)
{
echo "First";
}
else
{
echo "Fail";
}
?>
Something like this will do. For your result.php, use the following:
<?php
$eng= 40;
$mizo= 40;
$hindi= 40;
$maths= 40;
$ss= 40;
$science= 40;
// first group your variable into one array = $all
$all = array($eng, $mizo, $hindi, $maths, $ss, $science);
// second, just iterate over them till you find one value -40
for($i=0; $i < count($all); $i++){
if($all[$i] < 40) $sum = 1;
}
?>
For output:
<?php include "result.php";?>
<?php
$division=$row_['mark'];
$pass="Passed";
$test = (!empty($sum)) ? 'Failed' : 'Passed';
if($division>=80 && $pass==$test)
{
echo "Letter";
}
elseif($division>=70 && $pass==$test)
{
echo "First";
}
else
{
echo "Passed";
}
?>
You need to include the file first:
<?php
include "result.php"; //include the file
$division =$ row['mark'];
$pass = "Passed";
if($division == 80 && $pass == "Passed") {
echo "Letter";
}
elseif($division < 70) {
echo "Fail";
}
?>
<?php
$i=1;
while ($i<=$totalpages) {
if (($i>=($page-5) && $i<=($page+5) && $i<$totalpages) || $i==1 || ($i+1>$totalpages)) {
echo "<td><a href=\"search.php?page=$i\">";
if ($i=$page) {
echo "<strong> $i </strong>";
}
if ($i!=$page) {
echo " $i ";
}
echo "</a></td>";
}
$i++;
}
?>
Trying to build a webpage that ouputs some search values neat the bottom of the page, however I keep getting an infinite loop. I've no Idea about what's causing it, and would like someone elses insight into the problem at hand.
Your if condition has a problem
Change
$i =1
with
$i==1
You have a number of places that you reset $i, inadvertently I assume
$i=1
and
$i=$page
replace them with ==
<?php
$i=1;
while ($i<=$totalpages) {
if (($i>=($page-5) && $i<=($page+5) && $i<$totalpages) || $i == 1|| ($i+1>$totalpages)) {
echo "<td><a href=\"search.php?page=$i\">";
if ($i == $page) {
echo "<strong> $i </strong>";
}
if ($i!=$page) {
echo " $i ";
}
echo "</a></td>";
}
$i++;
}
?>
Changes in 1st and second if condition.
1) $i == 1
2) $i == $page
is there a reason why this can not be accomplished using a for loop ? While loops are always risky and easy to cause problems if there is a scenario to cause to loop for ever.
eg.
for ($i = 0; $i<=$totalpages; $i++){
// do something
}
There is some issues in the conditions
first: the
|| $i=1||
allways return true.
Change for
|| $i==1||
Second: The same case of adobe
if ($i=$page) //Allways return try is assignation operation
Change to:
if ($i==$page)
Got this code which is troubling me and am eager to accept any help!
$x=0;
while($row = mysql_fetch_array($result))
{
if ($me == $x)
{
echo "You are $me";
break;
}
else
{
$x++;
}
}
When I include the break; it returns :
You have selected the 1
However, when I remove the break; it returns
You have selected the 1 You have selected the 1 You have selected the 1 You have selected the 1
There are currently 6 records in the database and if the code were to work it would display "You are 4th"
Any ideas?
Your $x++; only executes on else. In order to have it increment on every iteration you need to remove the else:
$x=0;
while($row = mysql_fetch_array($result))
{
if ($me == $x)
{
echo "You are $me";
break;
}
$x++;
}
Just a side note: $row doesn't seem to be related to $me and $x here. I'll assume your loop contains some other code that you've omitted, but this alone will probably answer your question.
You are icrementing $x in the else, this is never getting executed.
You need to do the increment in the same part as the echo.
So either:
$x=0;
while($row = mysql_fetch_array($result))
{
if ($me == $x)
{
echo "You are $me";
$x++;
}
else
{
$x++;
}
}
Or if you never need to increment $x without echoing:
$x=0;
while($row = mysql_fetch_array($result))
{
if ($me == $x)
{
echo "You are $me";
x++;
}
}