i have a question that what is the need of elseif statement if our work can be done by using number of if. same output will be produced then what is the basic purpose of elseif? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

i have a question that what is the need of elseif statement if our work can be done by using number of if. same output will be produced then what is the basic purpose of elseif?

5th Aug 2016, 3:23 AM
Ankita Sharma
Ankita Sharma - avatar
10 Answers
+ 1
because an elseif statement isn't check if the if statement is True like if (1 == 1) { echo 1; } elseif (2 == 2) { echo 2; } return 1 and if (1 == 1) { echo 1; } if (2 == 2) { echo 2; } return 12
5th Aug 2016, 5:02 AM
francis
0
you need elseif statement if you have more than two condition in your code.
5th Aug 2016, 9:46 AM
simbiosacircle
0
thanks francis.. i got it
5th Aug 2016, 9:51 AM
Ankita Sharma
Ankita Sharma - avatar
0
elseif is used when u have to check for more than one independant condition
5th Aug 2016, 4:31 PM
Sumitabha Banerjee
Sumitabha Banerjee - avatar
0
U can also use switch instead of elseif. It is much more efficient
5th Aug 2016, 5:22 PM
Sumitabha Banerjee
Sumitabha Banerjee - avatar
0
sumitabha...can u explain ur point with an eg. plz
5th Aug 2016, 8:26 PM
Ankita Sharma
Ankita Sharma - avatar
0
I can give you examples, but u wont be satisfied, because switch compares the variable with every individual value that it can hold, and the code will be extremely tedious to write if the variable has to be checked within a large range. In that case elseif serves the purpose much more efficiently and cleanly.
6th Aug 2016, 8:03 AM
Sumitabha Banerjee
Sumitabha Banerjee - avatar
0
Eg. if(marks<30) echo fail; else if(marks>=30 && marks<50) echo pass; else if (marks>50) echo good;
6th Aug 2016, 8:04 AM
Sumitabha Banerjee
Sumitabha Banerjee - avatar
0
Switch only works more efficiently if the variable is compared with a small no. of values, otherwise in all cases elseif is better
6th Aug 2016, 8:06 AM
Sumitabha Banerjee
Sumitabha Banerjee - avatar
0
multiple if's are costlier than elseif.
27th Aug 2016, 11:16 AM
Mohammed Irfan
Mohammed Irfan - avatar