delete if/else statement in php - 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.

Related

Following code is not reaching the else statement

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')))

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

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

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)

Using 'and' and 'or' in an if/else PHP statement

I am attempting to use both AND and OR statements in my IF/ELSE statement, but I cannot get the desired effect.
What I would like to achieve is that if either 'a' or 'b' has a value of '1' but both 'c' and 'd' must be 1 then I get 'Yes'.
All my attempts have given me either 'Yes' or have not worked (blank screen).
<?php
$a = "0";
$b = "1";
$c = "1";
$d = "1";
if (($a == "1") || ($b == "1") && ($c == "1") && ($d == "1")) {
echo "Yes";
}
else {
echo "No";
}
?>
Thank you.
You need and extra parenthesis, to make sure the evaluation order will be done correctly, like in math:
if ( ( ($a == "1") || ($b == "1") ) && ($c == "1") && ($d == "1")) {
^ ^
That way, let's say for example:
$a = 1;
$b = 2;
$c = 1;
$d = 2;
The first parenthesis will be evaluated as true || false. The final result will be true.
So now you have true && ($c == "1") && ($d == "1")
$c = 1, so again, the next evaluation will be true && true && ($d == 1)
$d = 2, so the next round will be true && true && false, final result, in this example, will be false.
You need to add parenthesis.
Why?
Because inner parenthesis are evaluated first before outer parenthesis. Take this example:
((1 == 1 && (2 == 2)) || 3 == 3)
What will be evaluated first? The 2 == 2 then the 1 == 1 and then the 3 == 3. In your if condition, because you are mixing AND's and OR's, you will not get the desired affect.
( (($a == "1") || ($b == "1")) && ($c == "1") && ($d == "1") )
Should work for you. In fact you can do this so that it looks even better:
(($a == 1 || $b == 1) && $c == 1 && $d == 1)
Because it is not necessary to put 1 in quotes ie: "1". PHP's truthiness will evaluate 1 == "1" to be true. However if you wanted to check for an actual string that contains 1, then you would use the === operator.
$a = 1;
$b = "1"
$a == "1"; // true
$b == 1; // true
$a === "1"; // false
$b === "1"; // true
However for more information go here: http://php.net/manual/en/language.operators.precedence.php
The equality operators will be evaluated first, then &&, then ||. Parentheses will be evaluated before anything else, so adding them can change the order.
Check the answer In Java, what are the boolean "order of operations"?
It will always echo a Yes because PHP interpreter places The AND operation before the OR operation.
So your if statement interpretes like this:
If
a = 1 or b = 1 and c = 1 and d = 1
then
echo 'Yes'
else
echo 'No'
That's why you always get a yes..

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