NotionCommotion Posted December 26, 2020 Share Posted December 26, 2020 Why do I get the following results (specifically the second example)? I thought it would be documented under https://www.php.net/manual/en/language.types.string.php, however, the word "ampersand" doesn't appear. Also, looked at logical operators, but found nothing. Thanks function test(string $string){ printf('%s %s %s 0'.PHP_EOL, gettype($string), $string, $string==0?'==':'!='); } test('123'); test('@123'); test('1@23'); test('123@'); Quote string 123 != 0 string @123 == 0 string 1@23 != 0 string 123@ != 0 Quote Link to comment https://forums.phpfreaks.com/topic/311928-effect-of-string-starting-with-an-ampersand/ Share on other sites More sharing options...
Barand Posted December 26, 2020 Share Posted December 26, 2020 1 ) Why are you searching for "amprsand" when there are no ampersands (&) in your code. 2) the numeric value of a string is value of the numeric chars up to the first non-numeric character echo intval('12@3'); // 12 echo intval('@123'); // 0 1 Quote Link to comment https://forums.phpfreaks.com/topic/311928-effect-of-string-starting-with-an-ampersand/#findComment-1583421 Share on other sites More sharing options...
NotionCommotion Posted December 26, 2020 Author Share Posted December 26, 2020 Thanks Barand, No answer to #1, or at least no good answer. Regarding #2, never realized such and appreciate letting me know. Quote Link to comment https://forums.phpfreaks.com/topic/311928-effect-of-string-starting-with-an-ampersand/#findComment-1583425 Share on other sites More sharing options...
requinix Posted December 26, 2020 Share Posted December 26, 2020 Note that PHP 8 changed string <=> number comparisons. Quote Link to comment https://forums.phpfreaks.com/topic/311928-effect-of-string-starting-with-an-ampersand/#findComment-1583434 Share on other sites More sharing options...
NotionCommotion Posted December 27, 2020 Author Share Posted December 27, 2020 20 hours ago, requinix said: Note that PHP 8 changed string <=> number comparisons. Thanks requinix, Was going to comment on my reply to Barand that I felt PHP shouldn't be doing so, but didn't want to come off as whining. Looks like I will get what I want after all Quote Comparisons between strings and numbers using == and other non-strict comparison operators currently work by casting the string to a number, and subsequently performing a comparison on integers or floats. This results in many surprising comparison results, the most notable of which is that 0 == "foobar" returns true. This RFC proposes to make non-strict comparisons more useful and less error prone, by using a number comparison only if the string is actually numeric. Otherwise the number is converted into a string, and a string comparison is performed. Quote Link to comment https://forums.phpfreaks.com/topic/311928-effect-of-string-starting-with-an-ampersand/#findComment-1583449 Share on other sites More sharing options...
Barand Posted December 27, 2020 Share Posted December 27, 2020 20 minutes ago, NotionCommotion said: Was going to comment on my reply to Barand that I felt PHP shouldn't be doing so I hope you weren't planning on shooting the messenger. Quote Link to comment https://forums.phpfreaks.com/topic/311928-effect-of-string-starting-with-an-ampersand/#findComment-1583452 Share on other sites More sharing options...
NotionCommotion Posted December 27, 2020 Author Share Posted December 27, 2020 Just now, Barand said: I hope you weren't planning on shooting the messenger. Never a messenger who provides accurate information (which it currently is)! Quote Link to comment https://forums.phpfreaks.com/topic/311928-effect-of-string-starting-with-an-ampersand/#findComment-1583453 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.