Why is this if condition not working fine? - php

$isPass should be "Go to hell", which should differ from 0, yet, the code proves otherwise. Why?
<?php
$marks = 35;
$passing_marks = 36;
$isPass = ($marks > $passing_marks) ? true : "Go to hell";
echo $isPass; // prints "Go to hell".
if($isPass == 0){
echo "Pass"; // prints "Pass", but why and how
}
?>

Just try this.
<?php
$marks = 35;
$passing_marks = 36;
if($marks > $passing_marks){
echo "Pass";
}
else {
echo "Go to hell";
}
?>
You don't need the other variable, just execute the check on one if-statement.

Related

Display error when condition not matched inside foreach

I am displaying data from mysql using foreach but there is also a if condition inside the for each. I want to display a message if this condition is not matched. the code is below;
<?php
// some other codes to connect data base etc.
$i = 0;
while($row = mysqli_fetch_assoc($result)) {
$lon[$i] = $row['lng'];
$lat[$i] = $row['lat'];
$status[$i] = $row['status'];
$title[$i] = $row['title'];
$property_id[$i] = $row['property_id'];
$price[$i] = $row['price'];
$availability[$i] = $row['availability'];
$type[$i] = $row['type'];
$bedrooms[$i] = $row['bedrooms'];
$lounges[$i] = $row['lounges'];
$type[$i] = $row['type'];
$time[$i] = $row['time'];
$i++;
}
for($i=0; $i<$count; $i++) {
$distance = Haversine($my_lat, $my_lon, $lat[$i], $lon[$i]);
if($distance < $radius) {
echo 'Display data';
} else{
echo "error message";
}
}
?>
After displaying message following this code:
if($distance < $radius) {
I want to display message if this condition is not met but nothing seem to work so far.
if($distance<$radius){
echo "This statement meets the criteria.";
}else{
echo "This statement doesn't meet the criteria.";
}
You should check the basics on how if/else works before diving into this.
Before make sure that your datatype is define.
http://php.net/manual/en/function.settype.php
Try not allow empty data:
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
After verify mojore points, simplify your conditions by:
elseif($distance >= $radius){
Like this you avoid Unexpect values.
first you print $distance and $radius and after put if condition so you can confirm what actually happened...

Why doesn't subtraction sign work on while loop in php?

<?php
$hp = 0;
while($hp < 50) {
$flip = rand(0,2);
if ($flip == 1) {
echo "<p>X-Ray</p>";
$hp += 15;
} elseif ($flip == 2) {
echo "<p>Special Move</p>";
$hp += 10;
} else {
echo "<p>Punch</p>";
$hp += 5;
}
echo "<p>Total so far: $hp</p>";
echo "</br>";
}
?>
This is a PHP code. When I run it, it works fine. However, when I change it to this code below it doesn't.
<?php
$hp = 50;
while($hp > 1) {
$flip = rand(0,2);
if ($flip == 1) {
echo "<p>X-Ray</p>";
$hp -= 15;
} elseif ($flip == 2) {
echo "<p>Special Move</p>";
$hp -= 10;
} else {
echo "<p>Punch</p>";
$hp -= 5;
}
echo "<p>Total so far: $hp</p>";
echo "</br>";
}
?>
Please help. tHE CHANGES I MADE ARE THE HIGHLIGHTED ONES.
You never created $hp properly in the second version:
50;
doesn't do anything. It just tells php "here, have a 50", and php goes "gee, thanks, ok, whatever" and moves onwards. Then you have
while($hp > 1) {
Since $hp is undefined, it's null, and the code parses/executes as:
while($hp > 1) {
while(null > 1) {
while(0 > 1) {
FALSE -> exit loop
You never created $hp properly in the second version:
50;
If you do change it to $hp = 50;

How to execute php if condition once time only

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";
}
}

Is there any way something like this possible?

<?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 code how to clean user input from $_POST

I have this PHP code:
<?php
$score11 = $_POST['passmarks12'];
if($_POST['passmarks12'] > 100){
$grade11 = "";
}
elseif ($_POST['passmarks12'] < 45){
$grade11 = "Fail";
}
$strg = " $grade11";
echo $strg;
?>
The code is always printing "Fail" regardless of what is sent in.
I want it so that if it passes in blank or invalid input it fails.
And how should I properly cleanse the input?
Try this:
<?php
//$_POST['passmarks12'] = '';
if(empty($_POST['passmarks12']) || $_POST['passmarks12'] > 100)
{
$grade11 = "";
}
else if ($_POST['passmarks12'] < 45){
$grade11 = "Fail";
} else{
$grade11 = "Pass";
}
$strg = " $grade11" ;
echo $strg;
?>
Points:
Check if $_POST['key'] exists using isset.
Check if $_POST['key'] has a valid data type, string.
It may come as array.
Check if $_POST['key'] has a valid numeric format.
Compare $_POST['key'](String) and 45(Integer), using intval.
Example:
<?php
switch (true) {
case !isset($_POST['passmarks12']):
case !is_string($score = $_POST['passmarks12']):
case !is_numeric($score):
$result = 'Error (Invalid parameter)';
break;
case (intval($score) < 45):
$result = 'Fail (Less than 45)';
break;
default:
$result = 'Success (No less than 45)';
}
echo $result;
I think you want
elseif ($_POST['passmarks12'] < 45 && !empty($_POST['passmarks12']))
This PHP code will make sure $_POST is not empty before comparing it to 100.
<?php
$score11 = $_POST['passmarks12'];
if(empty($_POST['passmarks12']) || $_POST['passmarks12'] > 100)
$grade11 = "";
elseif ($_POST['passmarks12'] < 45)
$grade11 = "Fail";
$strg = " $grade11" ;
echo $strg;
?>

Categories