php - Else If block and curly brackets -
knowing in php ignore curly brackets in conditional blocks 1 function / line after "if", "else if" or "else" tag, :
if (myval == "1") dothis(); else if (myval == "2") dothat(); else donothing(); i asking myself if :
if (myval == "1") dothis(); else if (myval2 == true) dothat(); else donothing(); was seen php either :
if (myval == "1") { dothis(); } else { if (myval2 == true) { dothat(); } else { donothing(); } } or :
if (myval == "1") { dothis(); } else if(myval2 == true) { dothat(); } else { donothing(); } short version : "line break" after 1 else enough separate 1 "if" statement or seen 1 "else if"
editing own question confirmation guess :
if(false) echo "1"; else if (false) echo "2"; else echo "3"; is displaying "3" while following throwing error because 2 else statement found :
if(false) echo "1"; else if (false) echo "2"; else echo "4"; else echo "3"; so break line doesn't seems make difference, thing matters "if" , "else" count.
without curly brackets php treat next line block of code execute.
if (myval == "1") dothis(); else if (myval2 == true) dothat(); else donothing(); if myval "1" dothis(); next line , picked block of code execute. if there more code there:
if (myval == "1") dothis(); dothistoo(); it still execute dothis() part of if statement, whilst dothistoo() always executed.
if myval != "1" picks first block in else condition , executes that. in case if statement needs evaluated:
if (myval2 == true) this logic continues: identify next block of code execute either taking next line or next block identified wrapping of curly braces.
Comments
Post a Comment