Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'd like to know if my conditions are written with the right way (for the first statement). (in therm of optimization, readability)
if(($nb_post_by_user >= 3 && $nb_post_by_user < 5) || ( $nb_post_by_user >= 3 && ($nb_comm_by_user < 15 || $percent_voted < 25) )){
// Call function A();
}
else if( ($nb_post_by_user >= 5 && $nb_post_by_user < 10) && ($nb_comm_by_user >= 15 && $nb_comm_by_user < 30) && ($percent_voted >= 25 && $percent_voted < 70) ){
// Call function B();
}
Or does the first statement could be written that way? (second statement will be then nested).
if($nb_post_by_user >= 3){
if($nb_comm_by_user >= 15 || $percent_voted >= 25){
// Call function B
}
else{
// Call function A
}
}
if(($nb_post_by_user >= 3 && $nb_post_by_user < 5) || ( $nb_post_by_user >= 3 && ($nb_comm_by_user < 15 || $percent_voted < 25) )){
// Call function A();
} else if( ($nb_post_by_user >= 5 && $nb_post_by_user < 10) && ($nb_comm_by_user >= 15 && $nb_comm_by_user < 30) && ($percent_voted >= 25 && $percent_voted < 70) ){
// Call function B();
}
Edited one :
if($nb_post_by_user >= 3) {
if($nb_post_by_user < 5 || $nb_comm_by_user < 15 || $percent_voted < 25) {
// Call function A();
} else if($nb_post_by_user < 10 && $nb_comm_by_user >= 15 && $nb_comm_by_user < 30 && $percent_voted >= 25 && $percent_voted < 70) {
// Call function B();
}
}
In my opinion both the statements are not doing the same piece of work. The best logical reason i came up with is this :
The first piece of code will get executed if one of the condition written in the if statements are true. But in the second piece of code. The code will always gets executed if the first if condition holds true because of the presence of else statement instead of if else. So both are doing a different work.
hope it helps :)
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Building a simple algorithm where, based on various inputs (x, y, z), a phrase will be sent back to the user. What's the fastest way to store and search for these phrases?
Currently, I'm using various if statements. But I'm wondering if I should instead use some sort of dictionary, or hash maps.
I've posted a code sample here.
Thank you!
for ( $i=0 ; $i < count($age) ; $i++ ) {
if ($age[$i] >= 95) {
$something = "You need a plant";
} elseif ($age[$i] >= 85 && $age[$i] < 95) {
$something = "You need a small rug and a plant";
} elseif ($age[$i] >= 75 && $age[$i] < 85) {
$something = "You need a rug";
} elseif ($age[$i] >= 60 && $age[$i] < 75) {
$something = "You need a coffee table";
} elseif ($age[$i] >= 55 && $age[$i] < 60) {
$something = "You need a small table";
} elseif ($age[$i] >= 51 && $age[$i] < 55) {
$something = "You need a table";
} elseif ($age[$i] >= 42 && $age[$i] < 51) {
$something = "You need a knife";
} elseif ($age[$i] >= 35 && $age[$i] < 42) {
$something = "You need a knife and a jar";
} elseif ($age[$i] >= 22 && $age[$i] < 35) {
$something = "You need a knife, bowl, jar and a small book";
} elseif ($age[$i] >= 5 && $age[$i] < 22) {
$something = "You need a knife, bowl, jar and a book";
} else {
$something = "Go back to sleep";
}
if ($somethingElse[$i] >= 30) {
$also = " and we'll see you tomorrow!";
} else {
$also = ".";
}
$phrase[$i] = ''.$something.''.$also.'';
}
For the first part of the phrase, you could use an array with the age limits as keys and the texts as values. For the second part, which is more of a yes/no choice, you can use a ternary operator.
foreach is also nicer for looping over the $age array:
$phrases = [
95 => "You need a plant",
85 => "You need a small rug and a plant",
75 => "You need a rug",
65 => "You need a coffee table",
55 => "You need a small table",
51 => "You need a table",
42 => "You need a knife",
35 => "You need a knife and a jar",
22 => "You need a knife, bowl, jar and a small book",
5 => "You need a knife, bowl, jar and a book",
];
foreach ($age as $i => $a) {
foreach ($phrases as $limit => $something) {
if ($a >= $limit) break;
}
$also = $somethingElse[$i] >= 30 ? " and we'll see you tomorrow!" : ".";
$phrase[] = "$something$also";
}
Note that unless you already had an array $phrase, you don't need to specify the index $i, as [] will just make it append at the end. So in the end you only need $i for $somethingElse. The reason why it is still needed in the foreach syntax.
firstname and lastname are being submitted from a HTML form.
In PHP i want to check that both of them must be atleast 3 characters long and maximum upto 18 characters.
How can i check it in an if else statement
Example code is below
$f = $_POST['firstname'];
$l = $_POST['lastname'];
if((strlen($f) < 3 || strlen($f) > 16) && (strlen($l) < 3 || strlen($l) > 16))
{
echo "Firstname and lastname must be between 3 and 16 characters";
exit();
}
It is not working what i have done wrong.....
Thanks for help and I hope you understand my problem
if((strlen($f) < 3 || strlen($f) > 16) || (strlen($l) < 3 || strlen($l) > 16))
Replace && in the middle by ||
What you did only throws the error when both aren't valid.
You want to throw it when one of them is.
try this..
if((strlen($f) <= 3 && strlen($f) >= 18 ) && (strlen($l) <= 3 && strlen($l) >= 18 ))
{
echo "Firstname and lastname must be between 3 and 16 characters";
}
else
{
//query here..
}
I don't know why, but this doesn't seem to work.
Basically I want this to be true if the player1 ($playerX, $playerY) is within one square from player2 ($rs[x], $rs[y])
if (($rs[x] > $playerX-2 or $rs[x] < $playerX+2) && ($rs[y] > $playerY-2 or $rs[y] < $playerY+2)) {
// code
Any idea what I'm doing wrong?
If you replace with some test values, you might get:
if( (3 > 5 or 3 < 9) && (13 > 15 or 13 < 19))
Clearly, this is always true.
You should use && instead of or in this case.
It's just a case of replacing or with and. Here's a neater way of writing your if statement for bonus points:
if(abs($playerX - $rs[x]) < 2 && abs($playerY - $rs[y]) < 2) {
// code
Your conditions end up on true due to the OR operator
if (($rs[x] > $playerX-2 && $rs[x] < $playerX+2) && ($rs[y] > $playerY-2 && $rs[y] < $playerY+2)) {
//your code
}
I have:
if($this->itemCount <= 11 ) {
$item['subtotal'] = 12.95 * $item['qty'];
$item['price'] = 12.95;
$this->update_item($item['id'], $item['qty'], $item['price']);
}
But I need an "and" operator in there to check whether or not its also equal to or greater than 2.
if($this->itemCount <= 11 && => 2 )
I don't know how to do this in PHP. :(
if($this->itemCount <= 11 && $this->itemCount >= 2) {
// Your code here
}
if($this->itemCount <= 11 && $this->itemCount => 2 )
if($this->itemCount <= 11 && $this->itemCount >= 2 )
You just need to write the full expression out:
if($this->itemCount <= 11 && $this->itemCount >= 2 )
if I have something like:
if (($browser->getBrowser() == Browser::BROWSER_SAFARI && $browser->getVersion() >= 3 ) ||
($browser->getBrowser() == Browser::BROWSER_CHROME && $browser->getVersion() >= 5 ))
{
// code here
}
but I really want to say also if Chrome >= 5 but less 6...
I will add an else if for 6+
later on in else () less than version 5 would fall into..
How would I write >= 5 but < 6?
So you can do this directly by adding another condition:
if (($browser->getBrowser() == Browser::BROWSER_SAFARI
&& $browser->getVersion() >= 3 ) ||
($browser->getBrowser() == Browser::BROWSER_CHROME
&& $browser->getVersion() >= 5 && $browser->getVersion() < 6 ))
{
// code here
}
I'm assuming getVersion must be capable of returning non-integer numbers, otherwise you could just check for equality ($browser->getVersion() == 5).