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

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

Related

Arithmetic within parsed string [duplicate]

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);

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

Using an array without initialization in PHP [duplicate]

This question already has answers here:
Should an array be declared before using it? [closed]
(7 answers)
Closed 7 years ago.
In most languages, I have to initialize an associative array before I can use it:
data = {}
data["foo"] = "bar"
But in PHP I can just do
data["foo"] = "bar"
Are there any repercussions to doing this? Is this "the right way" to write PHP?
Is the same, but is not a good idea, the next is a copy-paste from php documentation.
If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize variable by a direct assignment.
Basically it's the same, and no you won't find any problem or repercussion.
But if you like you can do this:
$a = array();
You can read more in the PHP page

When should one use intval and when int [duplicate]

This question already has answers here:
Is there any particular difference between intval and casting to int - `(int) X`?
(7 answers)
Closed 8 years ago.
What is the better option:
// Option 1:
$intValue = (int) $numericValue;
// Option 2:
$intValue = intval($numericValue);
Is there any difference between these two lines, and which should be used in which situation?
intval is slower than casting to int (it's a function)
intval can accept a base if its first parameter is a string
Since the second item is really obscure, objectively we should say that intval is simply "worse".
However, personally I like reading intval($var) better than (int)$var (the latter may also require wrapping in additional parens) and since if you are converting to int inside a loop you are definitely doing something wrong, I use intval.
All behaviour explained here along with GOTCHAS...
http://php.net/manual/en/function.intval.php
http://www.php.net/manual/en/language.types.type-juggling.php

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