Where did i go wrong in if-statement php? - php

Need little help, always get the last value 188490?
if(isset($_POST['zapremina']) and is_numeric($_POST['zapremina']))
{
if ($_POST['zapremina']<=1050)
$_POST['zapremina']=1030;
if (($_POST['zapremina']>=1151) and ($_POST['zapremina']<=1300));
$_POST['zapremina']=2010;
if (($_POST['zapremina'] >= 1301) and ($_POST['zapremina'] <= 1600));
$_POST['zapremina']=4400;
if (($_POST['zapremina'] >= 1601) and ($_POST['zapremina'] <= 2000));
$_POST['zapremina']=9110;
if (($_POST['zapremina'] >= 2001) and ($_POST['zapremina'] <= 2500));
$_POST['zapremina']=45000;
if (($_POST['zapremina'] >= 2501) and ($_POST['zapremina'] <= 3000));
$_POST['zapremina']=91200;
if ($_POST['zapremina'] > 3001);
$_POST['zapremina']=188490;
}
else
$_POST['zapremina']=0;
I think is a little mistake, does anybody knows, txanks a log

Yeah, you need to do else if.
if ($_POST['zapremina']<=1050)
$_POST['zapremina']=1030;
else if (($_POST['zapremina']>=1151) and ($_POST['zapremina']<=1300))
$_POST['zapremina']=2010;
else if (($_POST['zapremina'] >= 1301) and ($_POST['zapremina'] <= 1600))
$_POST['zapremina']=4400;
etc.
As to why: You're checking against your variable and then you set it to another value, which tends to be higher than anything you're checking against AFTER you assign it.
So anything that's higher than would produce 188490 in the end.
The else means: Stop comparing at the first match.
Furthermore you need to remove the ; after the if statements, because the ; means: End of operation. In this context it would mean End of If...that in turn means, that the following line will ALWAYS be executed, the value of your variable doesn't even matter at that point any more.

remove ; [semicolon] at the end of every if statements and used else if ladder in current scenario

if(isset($_POST['zapremina']) and is_numeric($_POST['zapremina']))
{
if ($_POST['zapremina']<=1050)
$zapremina=1030;
if (($_POST['zapremina']>=1151) and ($_POST['zapremina']<=1300))
$zapremina=2010;
if (($_POST['zapremina'] >= 1301) and ($_POST['zapremina'] <= 1600))
$zapremina=4400;
if (($_POST['zapremina'] >= 1601) and ($_POST['zapremina'] <= 2000))
$zapremina=9110;
if (($_POST['zapremina'] >= 2001) and ($_POST['zapremina'] <= 2500))
$zapremina=45000;
if (($_POST['zapremina'] >= 2501) and ($_POST['zapremina'] <= 3000))
$zapremina=91200;
if ($_POST['zapremina'] > 3001)
$zapremina=188490;
}
else
$zapremina=0;

I see at least two errors:
1) You have semicolons (;) after almost every if-statement line of code. This way these statements don't even affect anything and your POST-variable is set six times.
2) You set your POST-variable to new (higher) values and check afterwards with this new and higher value which is not what you try to accomplish I guess.
Also - as datasage already said - avoid writing to the POST-variable.

Related

Security of a simple numerical comparison

Ok I'm feeling like I'm going back not forward, can't even figure out by myself if a simple if statement is secure or not...
First of all let's say we get a variable from url GET method :
$my_number = $_GET['numb'];
Now we make this simple if statement:
if(($my_number >= 1) && ($my_number <= 12))
{
put $my_number in database without escaping it
}
So the question would be - Can user pass this if condition with something else besides 1-12, I mean using hex numbers, commenting, doing that kind of stuff?
To validate a number use intval()
$my_number = intval($_GET['numb']);
Nothing but a number will be allowed.
This will also insure the value will not create an error in the SQL.
I do not like >= or <=
if(($my_number >= 1) && ($my_number <= 12))
Change to:
if(($my_number > 0) && ($my_number < 13))
Your code is not fully secured. User can pass this if condition with something else besides 1-12.
You can test that with this simple code:
<?php
$my_number = $_GET['numb'];
if(is_numeric($my_number)){
if(($my_number >= 1) && ($my_number <= 12))
{
echo'User Can Pass';
}else{
echo'User Can Not Pass';
}
}else{
echo'User Can Not Pass';
}
?>
Now browse your site like http://example.com/?numb=8 or http://example.com/?numb=15 or http://example.com/?numb=7 Samurai
I think now you can find your answer. Thanks.

Having problems understand the MySQL IF() and ELSE()

I'm completely fine with PHP IF() and ELSEIF(). However today I started to work with MySQL commands, I have a long MySQL query and I can't understand how I modify it.
Here is the code:
$db->colums[] = "
IF(
(SELECT
IF(p.delivery = '1',IF(distance < deliverymilesto1, deliverysurcharge1,
IF (distance < deliverymilesto2, deliverysurcharge2,
IF (distance < deliverymilesto3, deliverysurcharge3,
IF (distance < deliverymilesto4, deliverysurcharge4,
IF(distance < deliverymilesto5,deliverysurcharge5,0)
)
)
)
), 0)
FROM products prod WHERE prod.id_product = p.id_product
) > 0,
(p.price + (SELECT
IF(p.delivery = '1',IF(distance < deliverymilesto1, deliverysurcharge1,
IF (distance < deliverymilesto2, deliverysurcharge2,
IF (distance < deliverymilesto3, deliverysurcharge3,
IF (distance < deliverymilesto4, deliverysurcharge4,
IF(distance < deliverymilesto5,deliverysurcharge5,0)
)
)
)
), 0)
FROM products prod WHERE prod.id_product = p.id_product
)),
(p.price)
)as pricedelivery";
What I need to do is create an ELSE statement if p.delivery equals '2', and add in the following code:
IF(p.delivery = '2', IF(find_in_set('".$outcoder."', deliveryoutcode1), deliveryoutcharge1,
IF(find_in_set('".$outcoder."', deliveryoutcode2), deliveryoutcharge1, deliveryoutcharge2, 0)), 0)
in PHP this is simple as it would be:
if($delivery = '1') {
...code here
} elseif($delivery = '2') {
...otherwise
}
But I just can't understand the logic of how to change my statment. Maybe my brain is tired because I'be been learning about normalisation today. Can anyone help me here, thanks.
EDIT: I have spent the entire morning cleaning up the MySQL, result of which is above, based on what I have so far learnt about MySQL.
However, I would now like to be able to rewrite this using case, based on the useful comments I've received. I have 5 such MySQL queries on my page and I wonder if someone can be kind enough to rewrite the above in caseformat, then I can use this to rewrite the other queries? Thank you.
SELECT
price +
case delivery
when '1' then
case
when distance < deliverymilesto1 then deliverysurcharge1
when distance < deliverymilesto2 then deliverysurcharge2
when distance < deliverymilesto3 then deliverysurcharge3
when distance < deliverymilesto4 then deliverysurcharge4
when distance < deliverymilesto5 then deliverysurcharge5
else 0
end
when '2' then
case
when distance < deliverymilesto1 then deliveryoutcharge1
when distance < deliverymilesto2 then deliveryoutcharge2
when distance < deliverymilesto3 then deliveryoutcharge3
when distance < deliverymilesto4 then deliveryoutcharge4
when distance < deliverymilesto5 then deliveryoutcharge5
else 0
end
else 0
end
FROM products
IF() as a function is not the same as the branching you're used to. The first argument is a condition, the second argument is what is returned when the condition evaluates to true, the third is the opposite case when it's false.
Your code above just uses a lot of nesting in the third argument (the else branch) to accomplish what you'd normally do with IF...ELSE... or equivalently with the CASE expression.
You'll have to incorporate that into the rest of your query since you've left some of it out.

php if-else failing

What is wrong with this if-else statement.
if((strlen($objectData['pss'] >= 8))AND(strlen($objectData['pss'] <= 20)))
{
//do my bidding
}
else
{
echo "String to short or to long";
}
Ultimately I am trying to find if the variable is greater than or equal to 8 chars while being under or equal to 20 chars. My test string is 11 char, and I am seeing string to short/to long. I've done similar to this in the past, so I dunno what I mucked up at the moment (maybe Im just to tired to realize it)
if (strlen($objectData['pss']) >= 8 && strlen($objectData['pss']) <= 20)
if ((strlen($objectData['pss']) >= 8) and (strlen($objectData['pss']) <= 20))
{
//do my bidding
}
else
{
echo "String to short or to long";
}
I have corrected you brackets
Yes you are indeed "to tired".. You are basically counting the length of an expression instead of the string itself:
if((strlen($objectData['pss']) >= 8)AND(strlen($objectData['pss']) <= 20))

PHP ELSE IF statement

unable to get my else if statement to work, does anyone have any ideas? it works without the else if....
$waveFeet = round("$ar2");
if ($waveFeet >= 2) {
echo $waveFeet - 1;
}
else if ($waveFeet > 5) {
echo $waveFeet - 2;
}
else
{
echo "$wavefeet";
}
also as a side question, can anyone tell me how to change my round() to make it always round (down) instead of rounding up or down...?
Using the third argument of round you can round it down
echo $waveFeet = round($ar2, 2, PHP_ROUND_HALF_DOWN);
and for your if your else if condition will never got true as if the $waveFeet is greater than or equal to 2, the first condition will be true hence your elseif condition will never be true.
You should be changing it to
if ($waveFeet > 5) {
echo $waveFeet - 1;
}
else if ($waveFeet >= 2) {
echo $waveFeet - 2;
}
else
{
echo $wavefeet;
}
Try the statement with a particular value, say $waveFeet = 10;, then step through the code. The first condition succeeds, so the later branches are never checked. In fact, the only time the first branch isn't entered is when $waveFeet < 2, in which case the last branch body will be executed. Thus the middle branch is never executed. The more exclusive case should come first:
if (5 < $waveFeet) {
...
} elseif (2 <= $waveFeet) {
...
} else {
# $waveFeet < 2
...
}
To be completely safe, you can specify both boundary conditions:
...
} elseif (2 <= $waveFeet && $waveFeet <= 5) {
...
The inefficiency due to redundancy is minimal and the code is clearer. As you get more experienced, you can leave off this sort of thing.
If you wish to round even negative numbers down, use floor. If you wish to round towards zero (i.e. truncate), cast to an int:
$waveFeet = (int) $ar2;
you can use floor for down round
$waveFeet = floor($ar2);
if ($waveFeet > 5)
echo $waveFeet - 2;
else if ($waveFeet >= 2)
echo $waveFeet - 1;
else
echo $wavefeet;
you have to first check for 5 bcz 5 is big no than 2 and your first condition >= 2 also satisfied if no >5 so control go to first condition rather than second....
Theres nothing wrong in your code ( except that you can pass argument to function without quotes ). The way you are checking it is wrong.
it wont go to else if condition because the condition will be satisfied in the first check itself .
PHP buil-in floor() and ceil() functions round a number down and up respectively. I recommend posting the error you get so we can help you faster =)
Try this:
$waveFeet = floor($ar2);
if ($waveFeet >= 2 && $waveFeet <= 5 ) {
echo $waveFeet - 1;
} else if ($waveFeet > 5) {
echo $waveFeet - 2;
} else {
echo $wavefeet;
}
Note the change in the first condition (added && $waveFeet <= 5)
I think the problem might be that the ranges you use in your first and second conditions are overlapped, and it is very likely that in the case, let's say, $waveFeet == 6 PHP evaluates your first condition (originally $waveFeet >= 2), and it happened to be true, so PHP does not test the else if statement... Whenever it's possible to use disjunct conditions, I recommend you to do it...

change a variable based on even/odd status of another variable?

for($i=0;$i<$num;$i++) {
if($i==even) $hilite="hilite";
dothing($i,$hilite);
}
This is basically what I want to accomplish.
What is the most efficient way to determine if $i is even?
I know I could check if half == mod 2 ... but that seems a little excessive on the calculations? Is there a simpler way?
if ($i % 2 == 0)
The already mentioned % 2 syntax is most used, and most readable for other programmers. If you really want to avoid an 'overhead' of calculations:
for($i = 0, $even = true; $i < $num; $i++, $even =! $even) {
if($even) $hilite = "hilite";
dothing($i,$hilite);
}
Although the assignment itself is probably more work then the '%2' (which is inherently just a bit-shift).
It doesn't get any simpler than $i % 2 == 0. Period.
Change the i++ in the loop statement to i+=2, so that you only examine even values of i?
Typically, a number is odd if it's LSB (Least Significant Bit) is set. You can check the state of this bit by using the bitwise AND operator:
if($testvar & 1){
// $testvar is odd
}else{
// $testvar is even
}
In your code above, a more efficient way would be to have $i increment by 2 in every loop (assuming you can ignore odd-values):
for($i=0;$i<$num;$i+=2){
// $i will always be even!
}

Categories