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

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

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

Confusion in comparing string with a number in php [duplicate]

This question already has answers here:
Comparing String to Integer gives strange results
(5 answers)
How does PHP compare strings with comparison operators?
(4 answers)
Closed 3 years ago.
This is my code
$preference = '151000';
$range = 'above';
if($preference <= $range){
echo "Yes"; die;
}else{
echo "No"; die;
}
This provides 'Yes', i want to know why.
You can see it in the php manual. https://www.php.net/manual/en/language.operators.comparison.php
When comparing a string, number or resource with another string, number or resource:
Translate strings and resources to numbers, usual math
Btw: '151000' is a string, not a number. 15100 would be a number.
Here you basically compare two strings and php uses their ASCII codes to compare them. The first symbol 1 is lower than 'a'.
If you want to compare two strings properly, use function:
strcmp()
If you want compare different types, you can read about PHP type comparison tables.
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.
Read more here.

Conditional in PHP [duplicate]

This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 8 years ago.
When I run the follow code in PHP
if('yes' == 0)
echo 'a';
else
echo 'b';
The output is a.
I don't understand what happen?
And can I convert the php code to C source code to have a look what real happening?
PHP is a dynamically typed language, and == is a loose comparison operator, meaning it will first cast values it compares to one type, int for that matter, and then compare them; strings are being cast to integers by taking numericals from the left part, so 1abc casts to 1. By that logic yes cast to 0, and 0 == 0 yields true.

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

Categories