Arithmetic within parsed string [duplicate] - php

This question already has answers here:
How to increment a number inside foreach loop?
(5 answers)
Closed 7 months ago.
This post was edited and submitted for review 7 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I'm trying to do simple addition on a variable within a string, using string parsing, like so:
$a = 2;
echo("I have {$a+1} eyes");
But PHP crashes when I do this.
I also tried {++$a}, but that outputs as I have {++2} eyes.
The PHP manual page for string parsing has no example covering arithmetic within a string.
Is this something that's possible?
UPDATE: I have to disagree with the duplicate question flag. That question is titled "How to increment a number inside foreach loop?". While the answers to that question do also answer mine, search engines will not find that question from a query like "How to increment in a PHP string", or "Arithmetic with PHP string interpolation". Even if the answers are the same, the questions are different.

It's not possible through variable interpolation. It does not accept operations as arguments. What it does accept can be read in this question.
Alternatives:
Store the result into a variable first. A bit of a redundant step, but is a way to do it with interpolation.
$a = 2;
$b = $a + 1;
echo "I have {$b} eyes";
Use concatenation. Don't forget to wrap it in parentheses, because math operators don't have precedence over concatenation.
$a = 2;
echo "I have ".($a + 1)." eyes";
Use a format printing function. Note that printf outputs the string, while sprintf returns it, so you'd have to explicitly output the latter.
$a = 2;
printf("I have %d eyes", $a + 1);
echo sprintf("I have %d eyes", $a + 1);

Related

Why is 1...2 equivalent to "10.2" in PHP? [duplicate]

This question already has answers here:
Why does 1...1 evaluate to 10.1? [duplicate]
(4 answers)
Closed 3 years ago.
Just a second ago I was playing around with PHP, trying to figure out if there was a native range function (eventually finding range). However, one of the things I tried was the following:
echo 1...2;
which to my surprise returns the string "10.2". Can anyone tell me exactly what syntax is responsible for this? It doesn't seem like a valid place for a splat operator.
The statement consists of three parts: 1., . and .2. The first one evaluates to the number 1, the second one is the string concatenation operator, and the latter one evaluates to 0.2. Thus, you get 10.2.
Equivalent example code:
$a = 1.;
$b = .2;
echo "a = $a\n";
echo "b = $b\n";
echo "a.b = ".($a.$b)."\n";
outputs
a = 1
b = 0.2
a.b = 10.2

How to change data type to double from a Variable in PHP [duplicate]

This question already has answers here:
How to make numbers not be shown in scientific form?
(2 answers)
Closed 9 years ago.
I got a Variable $a as result from calculation from equations.
$a = 2.367760572051E-5
I need an output so the result of $a is 0.00002367760572051
So I try to convert the variable to double using this :
$result = (double)$a;
But the result is still the same,.
Is there any way to convert it?
If all you're after is a formatted string, try this one out
$result = sprintf('%0.17f', $a);
There's also
$result = number_format($a, 17);
if you want a result to print, try this.
$str = sprintf("%.20f",$a);
echo $str;
then you can result belows as.
0.00002367760572051
PHP is dynamically typed. Your variable $a is already a double internally. No need to typecast it again.
Use function sprintf to format the double as desired.

What does "++" mean in PHP? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I've been learning PHP by reading all sorts of things on the web. I've trying to figure out what this is called so I can look it up and read about it and learn how it works. It's the "++" in the code I'm assuming counts up so that the codes knows when to do a new line. I not looking for corrections to this code I'm looking for what the "++" is called so I can look it up.
$i = 0
if (++$i == 10) {
echo "new line";
}
It's called the prefix increment unary operator. It increments the number or character, then returns its value.
The postfix version does pretty much the same but returns its value first, then increments the variable.
That is a pre-increment. So in that code, $i is first incremented (increased by one), and then its value is used to test for equality to 10.
So the code that you show essentially tests if 1 equals 10 (which it does not.)
It is a number operator named the prefix increment. It takes the previous stated number and adds 1 to it. You can find more information about arithmatic operators here: Howtoforge.com
It's the pre-increment operator. It will add one to the variable it precedes, and return the result. The post-increment operator returns the current value, and then adds one. The PHP Manuel has an article on all of the operators.
All PHP Operators
Incrementing/Decrementing Operators

Which is faster - $a.$b or "$a$b" [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Speed difference in using inline strings vs concatenation in php5?
Note - This was an interview question.
Given,
$a = "some text 1";
$b = "some text 2";
Which one of the following will be faster. Give some reason
$c = $a.$b;
or
$c = "$a$b";
I answered that first one will be faster because two variables are just appended. In the second case however there are variable replacements within a string. But I am not sure.
It does not matter. The speed difference will be a few microseconds. It will never have any real-world impact whatsoever.
I would say option A
For basically the same reason you stated

PHP string operations [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why is the php string concatenation operator a dot (.)?
I've always wondered -
Why does PHP use the . operator to concat strings instead of the + sign?
Is this some sort of way to improve script-evaluation performance?
Thanks
Because otherwise, what would happen in this case?
$str = "4";
$num = 2;
$result = $str + $num;
What if you wanted the result to be "42"?
Clarification
The above answers the question "why is there an operator . in addition to the operator +?". If the intended question was "why does operator + not perform string concatenation?" (with the understanding that the would need to be another operator to take over the current behavior +), then I 'll be happy to remove my answer in favor of a more relevant one.
After deciding that it (PHP) would do lots of autoboxing there was pretty much no other choice then to use 2 different operators for "adding" and "concating".
"+" for adding is obvious and #Gumbo explained why "." was chosen.
var_dump("12ab" + "34cd"); // 46
var_dump("12ab" . "34cd"); // "12ab34cd"
so you need to tell the language that you want it to do because it can do both :)
Other languages don't have that problem because they don't allow the implicit conversion from a string to an integer.
So if you write "4" + 2 the language would tell you that it can't to that and you'd need to write: intval("4") + 2 and it knows what to do.
Also see here
why-is-the-php-string-concatenation-operator-a-dot
Because Perl used the . for string concatenation and PHP was highly influenced by Perl’s syntax.

Categories