why those two points are php tips? - php

i'm reading a blog that give something php coding tips.there are two places I don't understand.
less/not use continue.
the structure :
do{
if(true) {
break;
}
if(true) {
break;
}
} while(false);
is better than :
if(true) {
} else if(true) {
} else {
}
can somebody explain why ?

I highly doubt this as a do { } while () is checked every iteration, whereas an if is just a simple comparison.

Maybe the point they were trying to demonstrate in the blog is that a do {} while() loop will pause your code until a condition is met, where as the if statements will be parsed and your code will continue regardless of whether the condition is met or not.

Related

if condition best practice [duplicate]

This question already has answers here:
What's the difference between if and elseif?
(8 answers)
Closed 6 years ago.
I have the following functions:
if(A==B)
{
//do stuff
}
if(C==B)
{
//do stuff
}
if(A==B)
{
//do stuff
}
else if(C==B)
{
//do stuff
}
What is the difference between these two? I get the meaning of it but I have seen programmers prefer the first one instead of the 2nd one? why?
I personally prefer the 2nd one.
That's mostly a question of preference. I saw many programmers using both cases regardless of the actual function.
But correctly use the first case if you want both conditions to be chekced absolutely, the second one as a nested condition.
In the first case, both will be executed which makes it slower than the second one, as it checks only for the elseif, if the if condition is false
$var = 1;
if($var+1 ==2)
{
echo "test1";
}
if($var-0==1)
{
echo "test2";
}
if($var+1==2)
{
echo "test1";
}
else if($var-0==1)
{
echo "test2";
}
will output
test1test2test1
so we see, that number 2 ignores the elseif, which makes it faster.
In the first condition both the statements are excecuted because they are independent to each other, so all of them will be tested.
if(A==B)
{
//do stuff
}
if(C==B)
{
//do stuff
}
In second else if is just a nested if inside an else, so only one will of them will be tested, either if or elseif
if(A==B)
{
//do stuff
}
else if(C==B)
{
//do stuff
}
Not the same.
else if (C==B) will not be executed (even if C equals B) if the first condition was true.
without "else", just if (C==B) will be executed even if the first condition was true (in that case A==C :)
If only one variable is equal and need to run the same code regardless, it is better to use the first code so there is no loss of performance. But if you need to run different code depending on your variable, then the second is better.

PHP nested else statement without braces

I am working on legacy code I have inherited and I have come across this code.
This Code works!
Ignore the variable names look at the { the first Condition opens but never closes the else has no open? it still works?
Is it continuing on both or only one condition and if one which one?
Why does it even work?
foreach($bla as $foo) {
if(condition) {
if(condition) {
// Do Something
}else
continue;
}
}
My assumption is that the inner condition will be using the else? but I am only assuming that because of this answer: Nested if-else behaviour without braces I cannot find a definitive answer for PHP
You may rephrase this code (with proper indentation usage):
foreach($bla as $foo) { // FOREACH
if(condition) { // IF#1
if(condition) { // IF#2
// Do Something
} else continue; // END IF#2
} // END IF#1
} // END FOREACH
Now answers on your questions:
Is it continuing on both or only one condition and if one which one?
It is continueing only if nested if() (IF#2) statement condition evaluated to false.
Why does it even work?
It works because if() allows syntax without brackets for simple operations. See example 1 at the manual. So nested if() closed exactly by continue operation with following delimiter ;.
Yes, the else applies to the inner condition. The code compiles because the else block has no opening brace, so the braces are still balanced. Here's the same code properly indented:
foreach($bla as $foo) {
if(condition) {
if(condition) {
// Do Something
}
else
continue;
}
}
Of course this is inconsistent and a very bad way to write this code.
Surely it means that if the first IF condition is true, it will then go to the second IF condition.
So the second IF condition will only be read if the first one is true.
foreach($bla as $foo) {
if(condition) {
if(condition) {
// Do Something
}else continue;
}
}
If the "else" (or if, for that matter) is a one-liner, it doesn't need braces. The "else" in question is continuing the inner "if".

Simple php search form with break

I have a simple system for search and compare results.
In this case I'll have 2 results ok and one bad.
For use this I use the following code:
for ($i=0;$i<sizeof("search.txt");$i++)
{
if ($_REQUEST['search']=="ok" && $_REQUEST['car']=="")
{
print "ok";
break;
/// show results;
}
if ($_REQUEST['search']=="ok" && $_REQUEST['car']=="" && $_REQUEST['city']=="")
{
print "ok";
break;
/// show results;
}
}
The problem it's if the result must show 2 results, the line "break" stops the loop, but if I don't put break, it shows me results of other conditional "if", I put simple example, because into this loop I have many conditionals "if", by this I need to show all results in each case and break for each conditional, but break cur me the loop into the loop and no let continue
My question is if I can use break for do or need use other function let me do this, because I need do by this way
Thanks Regards
break is intended for case statements and loops. Since you're not using a case, the break will break the foreach. You probably want continue to move on to the next iteration.
e.g.
for(...) {
if (something) {
...
continue; // "break" this loop iteration and move on to the next one.
}
if (something else) {
...
break; // kills the foreach and moves down to the "stuff after loop" below.
}
switch (this_and_that) {
case 'a': ...; break; // breaks out of the switch and moves to "stuff after switch"
case 'b': ...; break; // ditto
}
... stuff after switch ...
}
... stuff after loop ...
I would make it this way:
if ($_REQUEST['search']=="ok" && $_REQUEST['car']=="")
{
print "ok";
if ($_REQUEST['city']=="")
{
break;
}
break;
}
because once first if-statement is true, the second will only need 'city' to be true.

PHP - Could this if statement EVER return true?

I'm trying to edit my co-worker's code (he's on vacation and out of reach) and there is this if statement:
if($this->carrier() == 1 and $this->carrier() == 2) {
return 'V';
}
My hope is that he accidentally put "and" instead of "or" which could explain a bug I'm getting, but he knows much more about PHP than I do and I want to be sure that this is wrong instead of just counter intuitive.
Yes, since it's a function with potential side effects, it might be true.
For example:
function carrier() {
return $someValue++;
}
Yes. The carrier() method could increment whatever value it returns each time you call it.
There's a small chance it could, yes.
He's calling a function twice, and you've not included the text of that function. So that could be doing something we can't see, like counting the number of times it's been called by this process.
On the other hand, it's much more likely that it is indeed a typo.
It is possible. Here is a working example you can run.
class program
{
private $i = 1;
function carrier()
{
$this->i=$this->i+1;
return $this->i-1;
}
function run()
{
if ($this->carrier()==1 && $this->carrier()==2)
{
echo "works";
}
else
{
echo "doesnt work" . $i;
}
}
}
$prog = new Program();
$prog->run();

if / else simplification to ternary operator

Can this be done for the code below. Problem I see is that there are two statements in the else clause and I couldn't figure out a way to fit in the echo...I need to return a 1 or a 0. Perhaps someone knows a trick?
function empty_user($c)
{
if((int)!in_array('',$this->a,TRUE))
{
return 1;
}
else
{
echo $c;
return 0;
}
}
No ternary =/. Although you can simplify this a lot because once the function returns, it stops interpreting the function anyway, so you can eliminate the else.
function empty_user($c) {
if ((int)!in_array('',$this->a,TRUE)) return 1;
echo $c;
return 0;
}
you generally shouldn't use ternary operators to determine execution order, but also, no, you won't be able to convert the if/else you've got there.
You can't use a ternary operator if you want more than one operation in either block, but the question is why would you want to? It is much clearer and easier to update if you have full blocks that you can continue to add code to.
in_array returns a bool which is perfect for an if statement - there is no need to cast it to an int.
function empty_user($c)
{
if (in_array('',$this->a,TRUE))
{
echo $c;
return 0;
}
return 1;
}

Categories