echo with print - unexpected answer [duplicate] - php

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.

Related

How to correctly 'print' out the actions of adding an array [duplicate]

This question already has answers here:
Remove foreach last comma PHP [duplicate]
(2 answers)
Closed 2 years ago.
This is what i have as my code currently:
<?php
foreach ($data_items as $d)
{
echo $d['x']." + ";
}
echo " = " .$sum;
?>
It is printing out as
2 + 3 + 4 + = 9
I need it to print without the last "+" sign. Ex:
2 + 3 + 4 = 9
Thanks for the help :)
.:Alter
<?php
$i = 0;
$len = count($data_items);
foreach ($data_items as $d)
{
if ($i == $len - 1) {
echo $d['x'];
}else{
echo $d['x']." + ";
}
$i++;
}
echo " = " .$sum;
?>

PHP - How to use check with function? [duplicate]

This question already has answers here:
How can I separate a number and get the first two digits in PHP?
(7 answers)
Closed 3 years ago.
Following are my PHP variables with values,
$value = 234333;
$value = 344665;
$value = 456325;
How to check if number start with 2 or 3 or 4.
If value start with 2 print "this is 2"
If value start with 3 print "this is 3"
If value start with 4 print "this is 4"
You can treat the value as a string and then use substr to check the first character.
Something like this would suffice:
function checkStartsWith($value) {
$result = "";
switch (substr($value, 0, 1)) {
case "2":
case "3":
case "4":
$result = "this is " . substr($value, 0, 1);
break;
}
return $result;
}
How you could call it:
echo checkStartsWith(234333);
echo checkStartsWith(344665);
echo checkStartsWith(456325);
Outputs:
this is 2
this is 3
this is 4

"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.

Sum in echo php [duplicate]

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

PHP output string with variable? [duplicate]

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

Categories