This question already has answers here:
The 3 different equals
(5 answers)
Why doesn't PHP print TRUE/FALSE? [duplicate]
(3 answers)
Closed 5 months ago.
<?php
$x = 8;
$y = 8.0;
echo ($x === $y);
?>
When I change from === to == there is output 1(true) but why I don't receive any output while using ===?
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);
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I have a simple "if" code what works. In "array" it works but gets error "Undefined offset:". What is wrong?
code what works:
if (file_exists(glob($root . '/*/E0622.pdf')[0])) {
echo str_replace($root . '/', '', glob($root . '/*/E0622.pdf')[0]) ;
} else {
echo 'x.pdf - no-no-no!' ;}
but in array gets " Parse error: syntax error, unexpected 'if' (T_IF) in ":
'E0622' => 'E0622' . if (file_exists(glob($root . '/*/E0622.pdf')[0])) {
echo str_replace($root . '/', '', glob($root . '/*/E0622.pdf')[0])
} else {
echo 'x.pdf - no-no-no!' } ,
What will be wrong?
Tnx!
The if statement itself cannot be used the way you are trying to do.
However, a short-hand if-else statement exists. It is introduced for cases like yours.
It looks like this: conditional-expression ? value-when-true : value-when-false. This is an expression, so you can insert it everywhere you would insert any other expression.
$var = 7;
echo ($var == 7 ? "Var is seven" : "Var is not seven");
// Those parentheses are optional, but I added them for clarity.
It echoes "Var is seven".
This works:
'E0622' => 'E0622' . (file_exists(glob($root . '/*/E0622.pdf')[0]) ? str_replace($root . '/', '', glob($root . '/*/E0622.pdf')[0]) : 'x.pdf - no-no-no!'),
PS: While you can nest as many short-hand if-else statements as you want, things may start to get really messy:
$a = 1 == 1 ? 2 == 2 ? 3 == 4 ? 9 : 8 == 8 ? 1 : 2 : 7 : 6;
The if construct in PHP is not an expression, and cannot be used in concatenation.
You need to change your structure to setting a variable to the thing you want to concatenate first and then adding that variable to the string.
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:
PHP is confused when adding and concatenating
(3 answers)
Closed 8 years ago.
I don't understand why it output -2yyy !
How can I output xxx8yyy ?
$ten = 10;
$two = 2;
echo "xxx".$ten - $two."yyy"; //-2yyy
echo "xxx".($ten - $two)."yyy";
Try this.
Just add brackets:
<?php
$ten = 10;
$two = 2;
echo "xxx".($ten - $two)."yyy"; // xxx8yyy