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.
Related
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 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:
Why does echo combined with printf displays wrong output? [duplicate]
(3 answers)
How to concatenate strings with function calls while using echo?
(6 answers)
Closed 11 months ago.
How do you insert a printf comment inside of an echo? I have been trying to figure it out for the past hour and everything I try results in a different error.
Below is the last version of the code that I tried.
<?php
$i = 0;
do {
echo "<li><a class=\"th radius\" href=\"img/coolstuff/" . printf("%03d", $i); . ".jpg\"><img src=\"img/coolstuff/" . printf("%03d", $i); . ".jpg\"></a></li>";
$i++;
} while ($i < 282);
?>
Remove the ; before the ., otherwise you're terminating the statement.
Additionally, printf directly outputs the string, which will make it appear at the start of the outputted string. You want sprintf instead.
Use sprintf() instead. It returns the formatted string instead of outputting it.
Also, as #Kolink mentioned, remove the semicolons, so your echo would look like this:
echo "<li><a class=\"th radius\" href=\"img/coolstuff/" . sprintf("%03d", $i) . ".jpg\"><img src=\"img/coolstuff/" . sprintf("%03d", $i) . ".jpg\"></a></li>";
Do like this:
echo "<li><a class=\"th radius\" href=\"img/coolstuff/" , printf("%03d", $i);
Notice the comma , instead of dot ..
When using a comma with a echo statement, like above, each part is evaluated first. Atleast according to this accepted anwer.
Can anyone please explain to me how this works:
<?php
print 5 . print 6 . print 7;
?>
it prints: 76151
I know the 1 is the return value from the print function, but why are the functions called in reverse order?
I believe this occurs because the dot operator is left-associative.
The expression would look like this with parenthesis:
print 5 . (print 6 . (print 7));
Your function is evaluating from right to left.
The trace is similar to this:
print (5 . print 6 . print 7)
print 7 evaluates first, printing 7 and returning 1.
print (5 . print 6 . 1)
This traces to print 61 and returning 1 Lastly:
print (5 . 1)
And thus you have 76151.
Your expression can be written like this:
print (5 . print (6 . print 7));
print 61 and print 7 are return boolean 1, thats why 1 is also printed