Sum in echo php [duplicate] - php

This question already has answers here:
Concatenation with addition in it doesn't work as expected
(2 answers)
Closed 7 years ago.
I'm a little bit discouraged because of PHP behavior.
Why does PHP interprets my code in this way:
echo "We has over " . 2500 + 500 . " employees over the world"; // 500 employees over the world

You can also change the above code into:
<?php
$sum=2500 + 500;
echo "We has over " . $sum . " employees over the world";
?>

Related

PHP zeropad (sprintf) AND number_format total [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 2 years ago.
I have a $total with a value of 1400 which I'm trying to echo as 1,400.00 however sprintf and number_format refuse to cooperate with each other. How do I properly format the $total to echo as 1,400.00?
<?php
$total = 1400;
echo sprintf('%0.2f',$total);
//'1400.00'
echo number_format($total);
//'1,400'
echo sprintf('%0.2f',number_format($total_grand));
//'1.00'
echo number_format(sprintf('%0.2f',$total));
//'1,400'
?>
This should suffice:
$total = 1400;
echo number_format($total,2);
This will output:
1,400.00
See: number_format()

Why addition can't be performed while echoing concatenation in PHP [duplicate]

This question already has answers here:
PHP is confused when adding and concatenating
(3 answers)
PHP concatenation of strings and arithmetic operations
(3 answers)
php concatenating string math operation
(4 answers)
Why do you have to add parentheses to + - operations when concatenating?
(1 answer)
Closed 4 years ago.
Edit: Sorry, wasn't aware that this question was already answered.
Consider this php code:
<?php
$var1 = 10;
$var2 = 12;
echo "The addition of " . $var1 . " and " . $var2 . " is " . $var1 + $var2 . ".";
?>
The output of the above code is:
12.
But, while doing any other mathematical operation like multiplication or division like done below
<?php
$var1 = 10;
$var2 = 12;
echo "The addition of " . $var1 . " and " . $var2 . " is " . $var1 * $var2 . ".";
echo "<br>";
echo "The addition of " . $var1 . " and " . $var2 . " is " . $var1 / $var2 . ".";
?>
This time, the output comes like this:
The addition of 10 and 12 is 120.
The addition of 10 and 12 is 0.83333333333333.
Of course, keeping the operation in brackets solves this, but I'm curious to why it showed only "12."

"if" working but in "array" it not working [duplicate]

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.

How to use 2 variables in For loop? [duplicate]

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>";
}
}

echo with print - unexpected answer [duplicate]

This question already has answers here:
Why does "echo '2' . print(2) + 3" print 521? [closed]
(4 answers)
Closed 9 years ago.
What is the output of the following code?
echo '1' . (print '2') + 3;
My answer was 15, but the answer is 214.
Why?
As executed, the code will do:
print '2' -> outputs 2
... print ALWAYS has a return value of 1, so the code becomes
echo '1' . (1 + 3); // with output '2'
This is simplified to
echo '1' . 4; // with output '2'
echo '14'; // output 2
final output: 214.

Categories