This question already has answers here:
Using nested ternary operators [duplicate]
(7 answers)
Closed 3 years ago.
I've tried the shorthand but it's now working properly on me please help me to solve this problem thank you.
$acc = "free";
echo $limit = $acc == "free" ? 5 : $acc == "muyip" ? 50 : 1000;
My expected result is 5 but the result is 50
use brackets as below
$acc = "free";
echo $limit = $acc == "free" ? 5 : ($acc == "muyip" ? 50 : 1000);
Related
This question already has answers here:
Test if number is odd or even
(20 answers)
Closed 4 years ago.
I am trying to create this algorithm with PHP. I want to be able to echo out the result of this operation:
1 - 2 + 3 - 4 + 5 - 6 +...100
I want to get the result of this till I get to 100.
This how I have already started the code, however I am stuck and don't know how to proceed:
<?php
$somme = 0;
$I = 1;
while($I <= 100){
}
?>
How do I go on from this?
All answers are appreciated
It's a case of knowing, within the loop, when to add and when to subtract. A simple tracker will help us here.
$somme = 0;
$i = 1;
$op = 'sub';
while($i <= 100){
$somme = $op == 'sub' ? $somme - $i : $somme + $i;
$op = $op == 'sub' ? 'add' : 'sub';
$i++;
}
As noted in the comments, you could also decide the operation based on whether $i is even or odd.
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
if($_POST) {
$type = $_POST['roll'];
if $type == 1 {
$number = rand(1,100)
if $number <= 50 {
echo "Winner!"
}
else {
echo "Loser!"
}
}
};
Can't figure out what is wrong. I am kinda new to PHP also. Type (or roll) is always 1 (for now)
I think you just need to put
if ($type == 1) {
instead of your actual statement, the condition of the if need that.
And so you'll need it too on the second "if"
Watch what your error is showing, it's exactly what you need.
This question already has answers here:
What are the PHP operators "?" and ":" called and what do they do?
(10 answers)
Closed 6 years ago.
$userauthorized = empty($_SESSION['userauthorized']) ? 0 : $_SESSION['userauthorized'];
I'm sure this is an elementary question and googling this is a nightmare.
What does the " ? 0 : " mean?
It's called a ternary operator and it is essentially a short-hand if() statement.
You're basically saying:
if(empty($_SESSION['userauthorized'])) {
$userauthorized = 0;
} else {
$userauthorized = $_SESSION['userauthorized'];
}
This question already has answers here:
The 3 different equals
(5 answers)
Closed 7 years ago.
This is what I've tried so far, but this still isn't working properly.
http://pastebin.com/w2wRaR6r
Still it's not working and I can't find why. Help please?
I've tried changing the header location method with meta refresh but that requires it to be in head, I think. Anyway thanks.
Simple - you were using a single = sign which sets a value ~ you need to use 2 to test for equality ( and missing semi-colon )
<?php
$r = rand(1, 3);
if ($r == 1) header('Location:http://www.google.com/');
if ($r == 2) header('Location:http://www.yahoo.com/');
if ($r == 3) header('Location:http://www.bing.com/');
?>
You are using = (which is assignment operator) and not == (which is comparison operator). It should be ==:
<?php
$r = rand(1, 3);
if ($r == 1)
header('Location:http://www.google.com/');
if ($r == 2)
header('Location:http://www.yahoo.com/');
if ($r == 3)
header('Location:http://www.bing.com/');
?>
This question already has answers here:
Compare floats in php
(17 answers)
Closed 9 years ago.
hello I have a problem wtich two variables in php. i have this code:
var_dump($total);echo '<br/>';
var_dump($reserva->getAdelanto());
if ($total == $reserva->getAdelanto()){
$total = 0;
echo "hello";
}
else
$total = $total - $reserva->getAdelanto();
print :
float(3940.2)
float(3940.2)
but does not enter the if when the two variables are equal. anyone knows why is that? greetings and thanks.
May be try with abs like
if ((abs($a)-abs($b)) <= 0.00001) {
echo "same";
}
Or
if (abs($a - $b) <= 0.00001) {
echo "same";
}
Or you also try like
var_dump( bccomp($a, $b) == 0 )
returns true if they are same