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
Related
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 for loop with 2 variables?
(7 answers)
Closed 6 years ago.
My PHP code (so far not working as I want it to) -
for ($x=0, $y=0; $x<=163, $y<=98 ; $x++, $y++) {
echo "Value of x = " . $x . " & y = " . $y . "<br>";
}
What I was trying to achieve is - it should echo even if one condition matches. Right now it stops when one condition is fulfilled, i.e. in this case when y=98, it stopped. Also there may be cases when y>=x in contrast to given code where x>y
Edit -
The duplicate marked question, did not solve my problem as in that question, range of both the variables were same, so it could have been achieved by the mentioned answer (by increasing one variable). In my case both variables has different range.
Also I tried this
for ($x=0, $y=0; $x<=163 || $y<=98 ; $x++, $y++) {
echo "Value of x = " . $x . " & y = " . $y . "<br>";
}
But it's also not helping me to achieve my desired output.
Edit 2 - I think I couldn't explain properly earlier, with what I wanted the output to be.
So I am trying to demonstrate by small e.g. with x=3, y=2
x=1, y=1
x=2, y=1
x=3, y=1
x=1, y=2
x=2, y=2
x=3, y=2
I am trying to achieve something like this. I don't know, what this ?matrix is called in maths (so it's possible my question title is wrong).
You can achieve that through a basic nested loop. Basically you loop over y in the outer loop and for each iteration of y you do a another for loop for x. Try this and adjust x and y as necessary.
<?php
for ($y=0; $y<=98 ; $y++) {
for ($x=0; $x<=163; $x++) {
echo "Value of x = " . $x . " & y = " . $y . "<br>";
}
}
PHP is an alien language for me. I am trying to pull some fields from WP's SQL database.. The content comes out okay but is a lot. I want to somehow put it in a HTML carousel or some slider, where data must be formatted like this:
<holder1>
<data1></data1>
<data2></data2>
<data3></data3>
</holder1>
<holder2>
<data4></data4>
<data5></data5>
<data6></data6>
</holder2>
I imagine I would have to put some for loop for i<4, do, then break into holder2.
Current script that echos the data =
while($row = mysql_fetch_array($rs)) {
echo "<div class='testimonials flexslider'><ul class='slides'><li class='testimonial flex-active-slide'><blockquote><p>" . $row['post_content'] . "</p><cite><span>" . $row['post_title'] . "</cite></span></blockquote></li></ul></div>"; }
I would like it to break after 3 <li> items each, into a seperate <div> or <article> whatever I find suitable according to the carousel I use.
You need an index variable, which echoes the beginning of the wrapper when it is divisible by 3, and the end of the wrapper when it leaves a remainder of 2 after division by 3 (and at the very end). It seems like this code would work:
$index = 0;
while($row = mysql_fetch_array($rs)) {
if ($index % 3 == 0) {
echo "<div class='testimonials flexslider'><ul class='slides'>";
}
echo "<li class='testimonial flex-active-slide'><blockquote><p>" . $row['post_content'] . "</p><cite><span>" . $row['post_title'] . "</cite></span></blockquote></li>";
if ($index % 3 == 2) {
echo "</ul></div>";
}
$index++;
}
if ($index % 3 != 2) {
echo "</ul></div>";
}
EDIT: a little bit of explanation to clarify the math. Suppose you have 10 results. Because we usually count from 0 in programming, they can be numbered 0 up to 9. This would make your structure look like this:
<holder>
<data0></data0>
<data1></data1>
<data2></data2>
</holder>
<holder>
<data3></data3>
<data4></data4>
<data5></data5>
</holder>
<holder>
<data6></data6>
<data7></data7>
<data8></data8>
</holder>
<holder>
<data9></data9>
</holder>
You see that we need a <holder> before elements 0, 3, 6 and 9 - all numbers which are divisible by 3. Mathematically, this is expressed with help of the remainder function - a number is divisible by 3 when its remainder after dividing by 3 is zero.
Likewise, we need a </holder> after elements 2, 5 and 8 - numbers which after division by 3 leave a remainder of 2.
We need to take care of the situation where the last block is not complete; that's why there's an extra block of code to take care of the last </holder>.
This question already has answers here:
Not getting the specific output without braces [closed]
(3 answers)
Closed 8 years ago.
This code only prints out '17' instead of "10 + 7 = 17". Why is this? And how can i solve this.
<?
$x = 10;
$y = 7;
echo $x . '+' . $y . '=' . $x+$y;
?>
What you should understand is that echo won't do anything until the result of the whole expression given to it is evaluated. This expression contains both . and + operators - the first one concatenates its operands (glues two strings together), the second adds numbers.
Now, you might think that + operator has a higher precedence than of . one - in other words, that result of $x + $y will be calculated before (eventually) glued to the rest of the string. But it's not. So we can say this statement is actually treated as...
echo ($x . '+' . $y . '=' . $x) + $y;
In other words, the result of all the string concatenation is added (converted to number) to $y, and only the result of this operation is printed.
The result of $x . '+' . $y . '=' . $x is '10+7=10', and it doesn't look like something that might be added. But guess what, PHP wants to be nice to you - and it assumes that you actually wanted to extract the first digits of a string when you tried to convert it to a number. So the whole line is treated as number 10. When added to 7, it's just 17 - that's why you got 17 echoed.
One possible workaround is getting rid of ., using , operator instead (as its precedence is lower - in fact, it's the lowest among operators):
echo $x, '+', $y, '=', $x + $y;
It could be simplified if one remember about such a handy feature of PHP as string interpolation:
echo "$x + $y = ", $x + $y;
<?php
$x = 10;
$y = 7;
function sum($a,$b){
return $a+$b;
}
echo $x.'+'.$y.'='.sum($x,$y);
?>
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.