PHP string operations [duplicate] - php

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.

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

Converting from a SQL Syntax to MongoDb Syntax Issues [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I have been doing my Homework regarding mongodb and PHP and honestly, I'm fairly new at this and this is my first post at SO.
What does"." operator do in PHP?
For example
$cmd = "SELECT m_time,m_latency,m_length FROM pkt_tbl WHERE m_in_port=". $in_port
What does "." in .$in_port mean? How can I convert the entire syntax to mongodb?
This is my try:
$db->pkt_tbl->find(array("m_in_port=".$inport,array("m_time"=>1,"m_latency"=>1,"m_length"=>1));
Please correct my syntax and enlighten me regarding "." operator, I badly want to learn and I'm a newbie at PHP and mongodb.
That is incorrect PHP. Try:
$db->pkt_tbl->find(
array("m_in_port"=>$inport),
array("m_time"=>1,"m_latency"=>1,"m_length"=>1)
);
In PHP, . is the concatenation operator. It tells the interpreter to stick strings or variables together end-to-end. For example, "hello " . "world" is equivalent to "hello world". In the case of your example, if $in_port=10, then your line of code would be equivalent to "SELECT m_time,m_latency,m_length FROM pkt_tbl WHERE m_in_port=10";

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

What are the different assignment operators in PHP, how do they differ [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I have seen references to = (of course) but also .= and ^=. What are those two for? Are there others?
^= is a bitwise operator and .= is a string operator. Both are assignment operators, as they set the value of a variable after evaluating.
The former sets the value of the variable to a XOR of the expression. The latter concats the expression onto the variable.
Many of the binary operators (e.g. +, -, *, /) can be used in conjunction with = as shorthand for assigning values. Essentially, x += 4 is equivalent to x = x + 4.

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

Categories