Following code is not reaching the else statement - php

Disclaimer: I'm new to programming, this is a simple exercise I'm doing so go easy on me.
The following code is not reaching (printing) the else statement. Any insight is much appreciated.
$number = '1120011';
// Decimal check
if($number[0] == '+' && $number[1] == '0'){
echo "Error: Can't assign a plus sign on a leading zero!";
// Leading zero check
} elseif($number[0] == '+' || $number[0] == '-'){
echo "Number is decimal";
// Binary check
} elseif (preg_match('~^[01]+$~', $number)) {
echo "Number is binary";
// Code not executing here. For e.g. $number = 1120011
} else {
"Number is non binary";
}
Another question:
Why is the following 'if' not working properly (if I replace it in the code above). I guess it has something to do with bad usage of operators.
if($number[0] == '+' || $number[0] == '-' && $number[1] == '0')
Thanks in advance! :)

For the second question:
if($number[0] == '+' || $number[0] == '-' && $number[1] == '0')
try
if(($number[0] == '+')||(($number[0] == '-')&&($number[1] == '0')))

Related

How to use multiple && ORs in a PHP If statement?

How I adjust this in order to get the brackets to work?
if (mysql_num_rows($printPricealertsdata) < 5 && ($_POST['pricetype'] == "pricebelow" && $_POST['price'] < $marketdata['priceusd']) OR ($_POST['pricetype'] == "priceabove" && $_POST['price'] > $marketdata['priceusd'])) {
echo "success";
}else{
echo "fail";
}
It would be easier if you told us what you want, and what you get with your code. But since you make me guess, I think you print "success" sometimes, even when the number of rows is 5 or more; and you don't want that. If that's the problem you need brackets around your OR-clause, because && binds closer. Check the manual for the precedence of different operators.
BTW you can use new lines and indentation to make your code more readable:
if ( mysql_num_rows($printPricealertsdata) < 5
&& (
($_POST['pricetype'] == "pricebelow" && $_POST['price'] < $marketdata['priceusd'])
OR
($_POST['pricetype'] == "priceabove" && $_POST['price'] > $marketdata['priceusd'])
)) {
echo "success";
} else {
echo "fail";
}
Try this (without placing brackets around each condition):
if(mysql_num_rows($printPricealertsdata) < 5 && $_POST['pricetype'] == "pricebelow" && $_POST['price'] < $marketdata['priceusd']) OR $_POST['pricetype'] == "priceabove" && $_POST['price'] > $marketdata['priceusd']) {
echo "success";
}else{
echo "fail";
}

What is the difference between !($x == ' ' OR $y == ' ') and $x != ' ' OR $y != ' '

What is the difference of between the following examples?
FIRST EXAMPLE
if($x != '' or $y != '' or $z!=''or $c!=''){
echo "<h3>YOUR INPUT:</h3>";
echo $x.'<br>'.$y.'<br>'.$z.'<br>'.$c.'<br>';
}
SECOND EXAMPLE
if(!($x == '' or $y == '' or $z==''or $c=='')){
echo "<h3>YOUR INPUT:</h3>";
echo $x.'<br>'.$y.'<br>'.$z.'<br>'.$c.'<br>';
}
Please explain. I'm newbie in programming. I couldn't get it when someone post it in my question and I saw the code I thought it was the same as the title, but I tried it and I saw the difference. Help me to understand this.
$x != '' or $y != '' or $z!=''or $c!='' is true if any of the variables are not empty. If any of the variables are abc or are otherwise not '', the condition is true.
!($x == '' or $y == '' or $z==''or $c=='') is true only if all of the variables are not empty. Another more readable expression of those conditions is:
$x != '' and $y != '' and $z != '' and $c != ''
use this code is better you know what is it mean x,y,z & c is not empty give result. || is mean or.
if(!empty($x) || !empty($y) || !empty($z) || !empty($c)){
echo "<h3>YOUR INPUT:</h3>";
echo $x.'<br>'.$y.'<br>'.$z.'<br>'.$c.'<br>';
}
The first example would work if any of the four variables is not empty.
The second example would work only when none of the four variables is empty.

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.

Set one variable to true based on conditions [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I set one variable to true based on other conditions. Here, instead of doing
if ($vara && $varb && $varc)
I'm something like below. Problem is, I'm just not getting something right. Can you please help me?
<?php
$onward = false;
$vara = 11;
$varb = 21;
$varc = 3;
if ($vara == 1)
{$onward = true;}else{$onward = false;}
if ($varb == 2)
{$onward = true;}else{$onward = false;}
if ($varc == 3)
{$onward = true;}else{$onward = false;}
if ($onward)
{
echo "Ok";
}else {echo "Not ok";}
?>
Each of your conditions ignores the result of the previous condition. You need to include the previous state of $onward in each subsequent test:
if ($vara == 1)
{$onward = true;}else{$onward = false;}
if ($varb == 2 && $onward)
{$onward = true;}else{$onward = false;}
if ($varc == 3 && $onward)
{$onward = true;}else{$onward = false;}
This way, varb and varc are only tested if $onward is still true after the previous test.
This is a particularly ugly way of writing code. If you have three large conditions and you don't simply want to join them on one line as in your $vara && $varb && $varc, you should be writing it this way:
$onward = ($vara == 1)
$onward = $onward && ($varb == 2);
$onward = $onward && ($varb == 3);
Any time you're simply returning/setting something to true/false in the branches if your if statement, you should just be returning/setting the condition itself.
That is, this:
if (condition) {
return true;
} else {
return false;
}
should always be written:
return condition;
The problem is, you are overwriting the value of $onward in every if-statement. Just use
$onward = $vara == 1 && $varb == 2 && $varc == 3;
I think you are looking for:
$onward = $vara == 1 || $varb == 2 || $varc ==3;
or
$onward = $vara == 1 && $varb == 2 && $varc == 3;
depending on your goal.
With this:
if ($varc == 3)
{$onward = true;}else{$onward = false;}
$onward will always be false if $varc is not 3.
I'm sure you mean
if (($vara == 1) && ($varb == 2) && ($varc == 3))
{$onward = true;}else{$onward = false;}
or even
$onward = (($vara == 1) && ($varb == 2) && ($varc == 3));
A better way of doing this:
if ($vara == 1) {$onward = true;} else {$onward = false;}
is this:
$onward = ($vara == 1);
You're overwriting the same variable each time, so the first two conditions are actually pointless.
I don't know why you'd want to do this over the first, more succinct approach, which will have the same logical effect:
if ($varc ==== 1 || $varc === 2 || $varc === 3)
$onward = true;
else
$onward = false;
Or even just:
$onward = $varc === 1 || $varc === 2 || $varc === 3;
Beware also of ever doing == 1. In comparisons, data are coerced to their truthy/falsy equivalents, so any truthy value will resolve to true in the comparison == 1. To test that something is literally 1, use ===. (This is a good rule generally unless you know you explicitly want to test for value and not type.)

give error if number is anything other than 1 or 0

<?php
$gender = 0;
if (($gender != 0) || ($gender != 1))
{
die('error:Must select a gender.');
}
?>
This should give a error if the gender is anything other than 1 or 0. So if i gave 5 it should die. If i gave it 1 it should not die. If i give it 0 it should not die.
I was thinking about a few work arounds
<?php
$gender = 0;
if ($gender == 0)
{
//number is okay
}
else if ($gender == 1)
{
//number is okay
}
else
{
die('error:Must select a gender.');
}
?>
Well that looks sloppy and it would work or i could create a array with 0 and 1 and check if its in it or not. Kinda overkill i think.
Not sure what i'm doing wrong.
You're using the wrong boolean operator:
if (($gender != 0) || ($gender != 1)) {
}
This will be entered if gender is 0, too, because it isn't 1, and vice versa. What you need to do is:
if (($gender != 0) && ($gender != 1)) {
}
Look at this table:
gender A (gender != 0) B (gender != 1) A || B A && B
----------------------------------------------------------------
0 false true true false
1 true false true false
5 true true true true
Also note Joshua's suggestion in the comments:
if (($gender == 0) || ($gender == 1)) {
/* number is ok */
} else {
die();
}
which is a bit longer, but more readable.
if (($gender != 0) && ($gender != 1))
^^
You want to die if both tests are true.
Change the or (||) to and (&&) and it should work, as you only want to fail, when it’s both not 0 and not 1.

Categories