php condition (in different value) - php

php condition (IN DIFFERENT VALUE)
if $tt > 29 print Good , if $tt < 55 print Very good but when $tt= less than 0 print very bad in php

<?php
if( $tt > 29 ) {
print "Good";
} else if( $tt > 29 && $tt < 55) {
print "Very Good";
} else if( $tt < 0) {
print "Very bad";
}
?>

Your question has too may loopholes and few numbers are overlapping your conditions and few are comming in no condition.
So here is a code which will print
very bad for number less than or equal to 0
bad for number 1 to 29
good for 30 to 55
very good for 55 and above
You can chenge number based on your needs
<?php
if( $tt <= 0 ) {
echo "Very Bad";
} else if( $tt <= 29) {
echo "Bad";
} else if( $tt <= 55) {
echo "Good";
} else {
echo "Very Good";
}
?>

Here you go
<?php
if( $tt > 29 ) {
echo "Greater Than 29 ";
} else if( $tt > 29 && $tt < 55) {
echo "Greater Than 29 and less than 55 ";
} else if( $tt < 0) {
echo "less than zero ";
}
?>

Related

I am getting the wrong result for all ages above 5. I cannot use switch statements

This is the code in question. It seems right to me but it for all ages above 5, it outputs "You can go to preschool"
<?php
$age = 13;
if ($age < 3) {
echo "You are too young for school";
} elseif ($age = 3 or $age = 4) {
echo "You can go to preschool";
} elseif ($age >= 5 and $age < 12) {
echo "You can go to primary school";
} elseif ($age >= 12 and $age < 18) {
echo "You can go to high school";
} else {
echo "You do not have to go to school";
}
?>
You need "==" for compare (just values) $var == $var2 or use "===" for more strict comparation (type and value).
The difference is this
<?php
$var = 1;
$var2 = "1;
echo $var == $var2; //Return 1 (true). Value comparation.
echo $var === $var2; //Return 0 (false). Value and type comparation [int not equal string, but still being 1 == "1"]
So your code should be like this (if age ever will be a int, else use "=="):
<?php
$age = 13;
if ($age < 3) {
echo "You are too young for school";
} elseif ($age === 3 or $age === 4) {
echo "You can go to preschool";
} elseif ($age >= 5 and $age < 12) {
echo "You can go to primary school";
} elseif ($age >= 12 and $age < 18) {
echo "You can go to high school";
} else {
echo "You do not have to go to school";
}
?>
The difference between the loosely == equal operator and the strict === identical operator is exactly explained in the PHP manual.

How to show grade according to marks in php?

code:
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if($grade <= 39)
{
echo '<span class="text-danger">Bad</span>';
}
else if($grade >=74)
{
echo '<span class="text-warning">Average</span>';
}
else if($grade >=100)
{
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>
Show grade according to:-
0-39 (Bad)
40-74 (Average)
75-100 (Good)
In this question I want to show message bad, average, good according to grade. Suppose if grade is 0-39 then it will show bad similarly if grade is 40-74 then show average like this but the condition I am giving is wrong. So, how can I do it?
Just change greater than to less than.
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if($grade <= 39)
{
echo '<span class="text-danger">Bad</span>';
}
else if($grade <=74) //Change to less than here.
{
echo '<span class="text-warning">Average</span>';
}
else if($grade <=100) //Change to less than here.
{
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
You need to modify the conditions so that no score is missed out of grade.
So, please define 3 ranges of scores using if and `else if'.
Range 1: 0-39: if ($grade <= 39) {
Range 2: 40-74: else if($grade <=74) {
Range 3: 75-100: else if($grade <=100) {
This way, first if checks if the grade is less than or equal to 39.
If yes, grade is Bad.
Else, if score, does not fit in this range, it will go ahead in next if else for the range: 40-74 and same way to 75-100 if it does not fit.
Corrected code:
if ($outoff!=0) {
$grade = ($score/$outoff)*100;
if ($grade <= 39) { // Score range: 0-39
echo '<span class="text-danger">Bad</span>';
}
// If $score is coming to this else if means it is definitely
// greater than 39: that is 40+
// Score range: 40-74 as it is in else if after if of `39`
else if($grade <=74) {
echo '<span class="text-warning">Average</span>';
}
// Score range: 75-100 as it is in else if after 0 - 39 and 40 - 74
else if($grade <=100) {
echo '<span class="text-success">Good</span>';
}
}
You have to make changes to you code as follow:
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if( $grade >= 0 && $grade < 40 ) {
echo '<span class="text-danger">Bad</span>';
}
else if( $grade > 39 && $grade < 75 ) {
echo '<span class="text-warning">Average</span>';
}
else if($grade > 74 && $grade <= 100 ) {
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if( $grade > 0 && $grade <= 39 ) {
echo '<span class="text-danger">Bad</span>';
}
else if( $grade >= 40 && $grade <= 74 ) {
echo '<span class="text-warning">Average</span>';
}
else if($grade >= 75 && $grade <= 100 ) {
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>

use OR in if codition to print reverse results in php

I am trying echo result "OK" if ( 1 either both ) variable true, i did so far like this
<?php
$user_id = $_SESSION['user_id'];
$point= "select points from users where id = $user_id "; // in db right now points = 2000
$flag= "select m_boost from users where id = $user_id "; // in db right now flag = 1
?>
<div class="box border">
<div class="box-title">
<?php
if($point < 1000 || $flag = 0) {
echo "not ok";
} else { ?>
echo "ok";
<?php }?>
</div>
it's working if i do like this
if(($point == '2000') || ($flag == '0') ){
but i don't want == operator for $point i want less than < $point < '999'
The problem is :
Keep getting result " Not Ok " even one variable (flag = 1) is true in db
Expected Results:
i want to print "OK" if $point > 1000 or flag == 1,
try this
when you using OR condition you should careful about condition and login. you should implement logic in if condition instead of else.
if($point > 1000 || $flag ==1) {
echo "ok";
}else {
echo 'Not ok'
}
or AS YOU WANT
if($point < 1000 ) {
echo "Not ok";
}else if($flag ==0) {
echo 'Not ok'
}esle {
echo 'ok'
}
or you can use this way
$a=false;
if($point < 1000 ) {
$a=true;
}else if($flag ==0) {
$a=true;
}esle {
$a=false;
}
// you can use this variable in your condition.
if($a) {
echo "ok";
}else {
echo 'Not ok'
}
When you are using OR, if the first condition is met the second is disregarded.
Also, make sure you use double equals (==) for comparison, not single equals (=) which means assignment.
Therefore you want to replace this:
if($point < 1000 || $flag = 0) {
With one of these:
SWAPPED AROUND
if($flag == 0 || $point < 1000) {
or
USING && INSTEAD
if($point < 1000 && $flag == 0) {
Depending on what behaviour you're looking for. It's a little unclear - so any additional clarification from you would be helpful.
Once you get something that you think is working, try to test all possible combinations so that you can be confident it works how you wish.
$flag = 0
You're setting the variable's value to 0.
Example:
$flag = 1;
if($a < $b || $flag = 0){ //$flag's value is 0 now.
...
...
}
In conditions comparisons, the right operator is "==".
if($point < 1000 || $flag == 0) {
List of comparison operators
but i don't want == operator for $point i want less than < $point <
'999'
Didn't fully understood your goals, but maybe:
For checking a value in a range the right logical operator should be "and" (&&);
if($point > 0 && $point < 999){
List of logical operators
Update:
if $point > 1000 or flag == 1
if($point > 1000 || $flag == 1){
echo "ok"
}
if (!$flag && $point < 1000)
{
echo "Not OK";
} else {
echo "OK";
}
Writing this into a truth-table:
flag point result
0 < 1000 Not OK
1 < 1000 OK
0 >=1000 OK
1 >=1000 OK
Worked for me now as per my question
i think we are all here for some contribution reason, flagging down a question is not a way, if you got solution than respond otherwise my question was 100% clear.

Convert 12 hours to 24 hours in PHP

This is my homework in which I was asked to convert time from 12 hours to 24 hours where time was provided in this format 05:09:15AM. I am new to programming, that's why instead of going into loops I decided to do it with conditional statements. So, I created 4 conditions (I used conditions as shown here)
What's the problem then? The problem is I am getting an error stating $_time variable is undefined when I am printing $_time. As per my understanding, this is happening because the $_time variable is inside the functions. But, if that's the case, can you guide me how to do this?
<?php
$_a = ("07:29:23PM");
$_a = explode(':',$_a);
if($_a[0] == 12 && $_a[1] <= 59 && strpos("PM", $_a[2] !== FALSE))
{
$_rpl = str_replace("PM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] < 12 && $_a[1] <= 59 && strpos("PM", $_a[2] !== FALSE))
{
$_a[0] += 12;
$_rpl = str_replace("PM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] == 12 && $_a[1] <= 59 && strpos("AM", $_a[2] !== FALSE))
{
$_a[0] = 00;
$_rpl = str_replace("AM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] < 12 && $_a[1] <= 59 && strpos("AM", $_a[2] !== FALSE))
{
$_rpl = str_replace("AM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
echo $_time;
?>
In your code there are few errors. The strpos syntax is wrong.
strpos("PM", $_a[2] !== FALSE) // this is incorrect
This you have to write
strpos($_a[2],"PM") //string first and search second.
This will return a integer, position of search string in the string, so don't use false instead use >-1
strpos($_a[2],"PM") > -1) //this is the correct method.
Also define $_time; in the starting and initialise it.
<?php
$_a = ("07:29:23PM");
$_a = explode(':',$_a);
$_time = ""; //initialised the variable.
if($_a[0] == 12 && $_a[1] <= 59 && strpos($_a[2],"PM") > -1)
{
$_rpl = str_replace("PM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] < 12 && $_a[1] <= 59 && strpos($_a[2],"PM")>-1)
{
$_a[0] += 12;
$_rpl = str_replace("PM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] == 12 && $_a[1] <= 59 && strpos($_a[2],"AM" ) >-1)
{
$_a[0] = 00;
$_rpl = str_replace("AM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] < 12 && $_a[1] <= 59 && strpos( $_a[2],"AM")>-1)
{
$_rpl = str_replace("AM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
echo $_time;
?>
Actually, initialising variable was not causing the error. Error was in your strpos syntax, so none of the if condition was true, so no code executed, so while trying to echo $_time; it was undefined. But its good practice to initialise a variable in the starting itself.
You have inbuilt functions to convert the datetime objects. You can refer php manual for that.
If you want to convert manually, you can do like this.
<?php
$_a = ("10:29:23PM");
$_a = explode(':',$_a);
if(strpos( $_a[2],"PM") > -1) //if PM given
{
$_a[2] = str_replace("PM","",$_a[2]); //remove the PM
if($_a[0] <12) //if time less than 12
$_a[0] = $_a[0] + 12; //then add 12 hours
}
if(strpos( $_a[2],"AM") > -1) //if AM given
{
$_a[2] = str_replace("AM","",$_a[2]); //remove AM
if($_a[0]=='12') //if 12 AM
$_a[0]='00'; //make it 0
}
$newtime = $_a[0].':'.$_a[1].':'.$_a[1];
echo $newtime;
?>
The main point is only changing on hour value, adding +12 if PM and change to 00 if AM.
<?php
$s = "07:05:45PM";
$s = explode(':',$s);
$time = substr($s[2],2,4);
$s[2] = substr($s[2],0,2);
if($time == "PM") {
if($s[0] != "12")
$s[0] = $s[0]+12;
}
if($time == "AM") {
if($s[0] == "12")
$s[0] = "00";
}
echo implode(":",$s);
?>
$string="10:29:23PM";
$a=substr($string, 0, 8);
$b= substr($string, 8, 10);
$dates=$a." ".$b;
// 12-hour time to 24-hour time
echo $time_in_24_hour_format = date("H:i:s", strtotime("$dates"));
// 22:29:23
echo $time_in_24_hour_format = date("H:i", strtotime("$dates"));
// 22:29

php. if conditions

This seems simple but I don't know why it doe snot work.
I need to write an if statement that
first, checks if it is numeric
second, if it is not between 1 and 10, issue errorA
third, if it is not between 20 and 30, issue errorB
fourth, it is not a number, issue errorC
If is not numeric and satisfies all the ranges, added to the database.
anyways, I am not sure about the if and while combination to satisfy this....
So far I have,
if numeric and satisfies ranges, add to database
else, issue errorC
How can I filter for error A and B?
if ( isset [some code...]) {
$a = ...;
$b = ...);
$c = ...;
if (preg_match('/^\d+$/',$b) && preg_match('/^\d+$/',$c) &&
((1 <= $b && 10 >= $b)) && ((20 <= $c && 30 >= $c))) {
$sql = "INSERT [some code...]
mysql_query($sql);
$_SESSION['success'] = $_POST['success'];
header('Location: index.php') ;
return;
} else {
$_SESSION['error'] = $_POST['error'];
header('Location: index.php') ;
return;
}
}
if (preg_match('/^\d+$/',$b) && preg_match('/^\d+$/',$c)) {
if (($b >= 1 && $b <= 10) && ($c >= 20 && $c <= 30)) {
echo "OK";
} else {
echo "not in range";
}
} else {
echo "not a number";
}

Categories