Jump to content

Effect of string starting with an ampersand


NotionCommotion

Recommended Posts

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

 

Link to comment
Share on other sites

20 hours ago, requinix said:

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.