Conditional Statements (IF, Else) not working properly in PHP [duplicate] - php

This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I am trying to apply some if else statements in my code but getting a bit confused. I want a result if I got value=0 answer should be One. if I got value=1 answer should be Two and if I got any value greater then 1 value should be Three. Following is my code: -
$value = 1
if($value < 1)
{
echo "One";
}
else if($value === '1')
{
echo "Two";
}
else if($value >= 2)
{
echo "Three";
}
The problem is that it is not giving me results if value is = 1.

change this else if($value === '1') to else if($value == 1)

=== matched both value and type that's why 1==='1' is false, See Ref.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.
<?php
$value = 1;
if($value < 1)
{
echo "One";
}
else if($value == '1')
{
echo "Two";
}
else if($value >= 2)
{
echo "Three";
}
?>
DEMO: https://3v4l.org/avOWc

Related

PHP If Statement is met display Either

I am familiar with using if else statement but my problem is how to display either if the condition is met.
if ($a == 1) {
echo 'B' OR 'C'; // just for reference
}
I have finally figure this out using nested loop
if ($a == 1) {
choiceresult = mt_rand(1,2)
if (choiceresult == 1) {
echo 'B';
}
if ( choiceresult == 2) {
echo 'C';
}
}
you have to fix your condition first :
if($a == 1)
Instead pf
if($a = 1)

how works or operator in if statements and print

I have two questions
1) how can i make php to check getnext() function,if it exists or what value it has ???
$a = 1;
if($a == 1 or getnext()== 1){
echo "yeap"; //this works
}
2)i want to write condition -- if $a or $b is equal to 1 , print the variable name that has the value of 1.
Is it possible to do in php ??can i do it this way???
if($a ==1 or $b==1){
print($a or $b);
}
thanks in advance:)
You could for example do as follows :
if($a ==1 || $b==1){
print (($a == 1)? $a : $b);
}
For your first question,
your getnext() function must be return some valid integer value then only you can compare it with integer 1.
for second,
You should write
if($a == 1 && $b == 1){
echo 'both are 1';
}
else if($a == 1){
echo '$a is 1';
}
else if($b == 1){
echo '$b is 1';
}else{
// both are not 1
}
also see this links,
logical operators or vs || (double pipe) in php
PHP: return value from function and echo it directly?
You write condition? You use if and else, if no else in if you put return. Goodluck

delete if/else statement in php

I am trying to learn PHP, and I am playing around with if loops, just to learn how to use them. I have made 8 variables containing a letter. After this I have made 8 if/else sentences that is writing these letters out.
<?php
$letter1 = 'N';
$letter2 = 'E';
$letter3 = 'K';
$letter4 = 'T';
$letter5 = 'A';
$letter6 = 'R';
$letter7 = 'I';
$letter8 = 'A';
if($letter1 == 'N') {
echo $letter1;
}
else {
echo 'FALSE';
}
if($letter1 == 'N' && $letter2 == 'E') {
echo $letter1.$letter2;
}
else {
echo FALSE;
}
if($letter1 == 'N' && $letter2 == 'E' && $letter3 == 'K') {
echo $letter1.$letter2.$letter3;
}
else {
echo FALSE;
}
if($letter1 == 'N' && $letter2 == 'E' && $letter3 == 'K' && $letter4 == 'T') {
echo $letter1.$letter2.$letter3.$letter4;
}
else {
echo FALSE;
}
if($letter1 == 'N' && $letter2 == 'E' && $letter3 == 'K' && $letter4 == 'T' && $letter5 == 'A') {
echo $letter1.$letter2.$letter3.$letter4.$letter5;
}
else {
echo FALSE;
}
if($letter1 == 'N' && $letter2 == 'E' && $letter3 == 'K' && $letter4 == 'T' && $letter5 == 'A' && $letter6 == 'R') {
echo $letter1.$letter2.$letter3.$letter4.$letter5.$letter6;
}
else {
echo FALSE;
}
if($letter1 == 'N' && $letter2 == 'E' && $letter3 == 'K' && $letter4 == 'T' && $letter5 == 'A' && $letter6 == 'R' && $letter7 == 'I') {
echo $letter1.$letter2.$letter3.$letter4.$letter5.$letter6.$letter7;
}
else {
echo FALSE;
}
if($letter1 == 'N' && $letter2 == 'E' && $letter3 == 'K' && $letter4 == 'T' && $letter5 == 'A' && $letter6 == 'R' && $letter7 == 'I' && $letter8 == 'A') {
echo $letter1.$letter2.$letter3.$letter4.$letter5.$letter6.$letter7.$letter8;
}
else {
echo FALSE;
}
?>
So the output of this is:
NNENEKNEKTNEKTANEKTARNEKTARINEKTARIA
I have to questions I hope you can help me with.
1: If I want the output to be:
N
NE
NEK
NEKT etc etc. How do I make the space between then lines. Fx like html
2: can I do anything to ignore my if/else statements afterwords? Fx if I only wants to write out "Nektaria" and not N, NE, NEK, NEKT etc. I could of course just delete them, but it is just to make some small assignments for myself.
Best Regards
Mads
To answer your first question about spaces, then you would have to add those in your echo
example:
echo $letter1.$letter2. ' ';
For you second question, you could use switch and sort of make a menu with numbers. Example if 4 was pressed then print out whatever letters.
<?php
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
http://php.net/manual/en/control-structures.switch.php
From this answer
PHP (typically) generates HTML output for a web-site.
When displaying HTML, the browser (typically) collapses all whitespace in text into a single space. Sometimes, between tags, it even collapses whitespace to nothing.
So you would just want to add a <br/> at the end of your echo statements like so:
echo $letter1.$letter2.$letter3.'<br/>'; //And so on for all the other echo statements
Now to answer the second part of your question, the if-else statements get executed sequentially. That is the whole reason why you see the output in that order. If you want to check for all the letters first (which outputs "NEKTARIA"), you'll just have to perform that check first and re-order your if-else statements. To ignore the if-else statements, you could use a simple $FLAG variable and set it to TRUE or FALSE depending on if it has performed the first check or not. So once you have performed the check you need, just do:
$FLAG = true;
And in all the if-else statements just check if $FLAG == false and only then proceed.
The switch statement as many have already pointed out, resolves these two issues directly and allows you to encapsulate multiple conditions and selectively check them.
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
The code is pretty intuitive and self explanatory. Here is the link of the official W3 Schools tutorial on it which could be a great starting point for you. Hope this gets you started in the right direction.
1) For making your output like this: N NE NEK NEKT
Make this change in the echo statement :
echo $letter1." ";
echo $letter1$letter2.$letter3." ";
and so on for printing a space between two variables all you need to do is add this part after the variable you want to have a space: ." "
2) And for making your out put like this: Nektaria
don't echo out multiple variables in each and every if else statement. just one variable in each if-else loop.

when i test this code , it was not echo `8 > 3`? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
when i test this code , it was not echo 8 > 3 (it's will echo 1 = 1)?
i check my code are correct but why not echo real result?
<?PHP
$number = 8;
if ($number = '0')
{
echo $number." = 0";
}
elseif ($number = '1')
{
echo $number." = 1";
}
elseif ($number = '2')
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
Comparison in PHP is done via the == operator, not the = operator.
<?PHP
$number = 8;
if ($number == '0')
{
echo $number." = 0";
}
elseif ($number == '1')
{
echo $number." = 1";
}
elseif ($number == '2')
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
Try this:
<?PHP
$number = 8;
if ($number == 0)
{
echo $number." = 0";
}
elseif ($number == 1)
{
echo $number." = 1";
}
elseif ($number == 2)
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
You were using assigning operator, instead of equal to operator, and secondly check with integer value instead of string.
The = operator assigns the value on the right hand side to the variable on the left.
if ($number = '0')
This sets $number to 0, which is a falsy value, so returns false.
elseif ($number = '1')
This sets $number to 1 which is a truthy value, which returns true. This is why your code outputs 1 = 1.
You need to instead use the == equality operator:
if ($number == '0')
...
elseif ($number == '1')
...
...
You are assigning a value to the variable $number, you'll need to use the == or better === to match the number against the value of the variable.
In this case you know $number is going to be an integer, it is best practice in PHP to compare as strict as possible, which is the === operator. You can use the == operator, however the variable will only be loosely matched at that point.
For example a boolean false will also match an if-statement containing if ($number == 0), which is not what you want.
Please check the documentation of PHP to see the full extend of comparison.
Such as the following:
<?PHP
$number = 8;
// See the triple equal sign here
if ($number === 0)
{
echo $number." = 0";
}
// and here
elseif ($number === 1)
{
echo $number." = 1";
}
// and here
elseif ($number === 2)
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>

if and else statement mixed up

I am doing the following if else statement below but number (//1) and number (//4) get executed at the same time, I am finding it abit hard to understand why.
<?php
//1
if($a == 1 && count($b) == 0) {
// do this
}
//2
elseif ($a == 1 && count($b) > 0) {
// do that
}
//3
if($a== 0 && count($b) == 0) {
// do a different thing
}
//4
else {
// do the last thing
}
?>
I have done this and it works but i think the should be a more suitable way for not using elseif for this.
else if($a== 0 && count($b) > 0) {
// do the last thing
}
but number (//1) and number (//4) get executed at the same time
It's because you don't have else before the if on //3
//3
if($a== 0 &&
Change to elseif($a== 0 &&
At the moment you have two separate IF conditions
You're missing a closing brace after your first if.
Also, you have a weird operator inside your first condition : $$. Maybe you intended to type &&?
$a = 10;
if ($a == 5) {
echo 'ok';
} elseif ($a == 10) { // $a is equal to 10, so it executes;
echo 'not_ok';
}
if ($a > 20) {
echo 'ok_ok';
} else { // $a is not >20 so else statement executes
echo 'not_not';
}
final result: not_oknot_not
If you are performing such tests on one and the same assignee, but different values, you might not want to execute more than one?
I guess you need elseif where third block is if
if ($a == 5) {
echo 'ok';
} elseif ($a == 10) { // $a is equal to 10, so it executes and stops the block;
echo 'not_ok';
} elseif ($a > 20) {
echo 'ok_ok';
} else { // $a is not >20, but the block was stopped on first elseif
echo 'not_not';
}
produces not_ok
Even if you move the else statement after the first elseif block as was suggested
if ($a > 20) {
echo 'ok_ok';
}
will execute, and if it's true, it will produce result, which again will result in double result
You might want to do this...
if ($a == 1 && count($b) == 0) {
// do this
}
//2
elseif ($a == 1 && count($b) > 0) {
// do that
}
elseif ($a== 0 && count($b) == 0) {
// do a different thing
}
else {
// do the last thing
}
The reason they get executed at the same time is that... Well technically they're not executed at the same time since it's procedural, but they both get executed because they are both different if else conditions. If you want only 1 execution, you should combine them :)

Categories