So i like to use return to 'stop' the code so to speak because i think it looks neater..but does returning null when nothing should be returned effect performance at all? example.. i like to use this.. PHP: if($foo !== $bar){ //nothing happens bc foo is not equal to bar return}//something does happen here bc foo is equal to barf($foo !== $foobar){ //nothing happens bc foo is not equal to foobar return}//something does happen here bc foo is equal to foobar But does that effect performance? should i use this?.. PHP: if($foo === $bar and $foo !== $foobar){ //do some stuff}else{//yadda yadda bad example but you get the question right? XD}
No difference at all. But in terms of readability, consider the following cases: Code: if(user is disabled){ Teleport user to disability's toilet; return; } if(user is male){ Teleport user to male's toilet; }elseif(user is female){ Teleport user to female's toilet; } // let's assume bisexual cases don't exist See the difference? When I check for the disabled state, I used an individual if-act-return state, because it is an exceptional case. Meanwhile, I used if-act-else-act for male and female, because they are parallel (of equal importance), not that one case is exceptional than another case. (I am not discriminating disability, but in this situation, it is indeed an exceptional case) Another case is like this (more practical this time): Code: if(sender is not player){ Warn to use this command only in-game; return; } Execute the command knowing that the sender is a player For cases using if-act-else-act, it is similar to a switch-case-default block. There is not a vase that is more important than another - they are just compared parallelly. However, remember semicolons
Got ya. Thanks! I just saw that a return with no value returns null..and if it is returning null when nothing should be returned i wondered if that may cause issues a or unneeded load on the server.
If it returns nothing, it returns void. If you try to assign the return value of a void function to a variable, the variable becomes null. If you ignore the return value of a void function, whatever you return would not matter. I am not sure about PHP, but let's see this comparison: Code: struct Stru{} a; int main1(){ int a = 0; return a; } int main2(){ int a = 0; } Stru main3(){ return a; } Stru main4(){ } compiled and then disassembled: Code: 00000001004010f8 <main1()>: 1004010f8: 55 push %rbp 1004010f9: 48 89 e5 mov %rsp,%rbp 1004010fc: 48 83 ec 10 sub $0x10,%rsp 100401100: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp) 100401107: 8b 45 fc mov -0x4(%rbp),%eax 10040110a: 48 83 c4 10 add $0x10,%rsp 10040110e: 5d pop %rbp 10040110f: c3 retq 0000000100401110 <main2()>: 100401110: 55 push %rbp 100401111: 48 89 e5 mov %rsp,%rbp 100401114: 48 83 ec 10 sub $0x10,%rsp 100401118: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp) 10040111f: 48 83 c4 10 add $0x10,%rsp 100401123: 5d pop %rbp 100401124: c3 retq 0000000100401125 <main3()>: 100401125: 55 push %rbp 100401126: 48 89 e5 mov %rsp,%rbp 100401129: 5d pop %rbp 10040112a: c3 retq 000000010040112b <main4()>: 10040112b: 55 push %rbp 10040112c: 48 89 e5 mov %rsp,%rbp 10040112f: 5d pop %rbp 100401130: c3 retq 100401131: 90 nop 100401132: 90 nop 100401133: 90 nop 100401134: 90 nop 100401135: 90 nop 100401136: 90 nop 100401137: 90 nop 100401138: 90 nop 100401139: 90 nop 10040113a: 90 nop 10040113b: 90 nop 10040113c: 90 nop 10040113d: 90 nop 10040113e: 90 nop 10040113f: 90 nop So just looking at main1() vs main2(), there is this extra instruction: Code: 100401107: 8b 45 fc mov -0x4(%rbp),%eax This merely copies a value to the %eax register, which is an extremely basic processor instruction, which basically means we can neglect the overhead. However, the situation is a bit different in PHP. In PHP, null is a constant, so PHP has to first retrieve the null value. But in the end, the difference is extremely negligible, so no need to worry.