logo

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ
No Result
View All Result
logo
No Result
View All Result
Trang chủ string function

STRING FUNCTION

by Admin _ May 02, 2022
It seems that PHP"s === operator is case sensitive. So is there a reason to use strcmp()?

Is it safe to vì something like the following?

if ($password === $password2) ...

*

*

The reason to lớn use it is because strcmp

returns 0 if str1 is greater than str2, & 0 if they are equal.Bạn sẽ xem: How to compare strings in php

=== only returns true or false, it doesn"t tell you which is the "greater" string.

Bạn đang xem: String function


*

*

You should never use == for string comparison. === is OK.

$something = 0;echo ("password123" == $something) ? "true" : "false";Just run the above code and you"ll see why.

$something = 0;echo ("password123" === $something) ? "true" : "false";Now, that"s a little better.


*

Don"t use == in PHP. It will not vì what you expect. Even if you are comparing strings lớn strings, PHP will implicitly cast them lớn floats và do a numerical comparison if they appear numerical.

For example "1e3" == "1000" returns true. You should use === instead.

Well..according khổng lồ this php bug report , you can even get 0wned.

It gives you a warning , but still bypass the comparison.You should be doing === as Always remember, when comparing strings, you should use === operator (strict comparison) & not == operator (loose comparison).

Xem thêm: header download php

Summing up all answers :

== is a bad idea for string comparisons.It will give you "surprising" results in many cases. Don"t trust it.

strcmp() should be used if you need lớn determine which string is "greater", typically for sorting operations.

Using == might be dangerous.

Note, that it would cast the variable to lớn another data type if the two differs.

Examples:

echo (1 == "1") ? "true" : "false";echo (1 == true) ? "true" : "false";

As you can see, these two are from different types, but the result is true, which might not be what your code will expect.

Using ===, however, is recommended as chạy thử shows that it"s a bit faster than strcmp() and its case-insensitive alternative strcasecmp().

Quick googling yells this speed comparison: http://snipplr.com/view/758/

strcmp() & === are both case sensitive but === is much faster

sample code: http://snipplr.com/view/758/

strcmp will return different values based on the environment it is running(Linux/Windows)!

The reason is the that it has a bug as the bug report says https://bugs.php.net/bug.php?id=53999

Please handle with care!!Thank you.

You can use strcmp() if you wish lớn order/compare strings lexicographically. If you just wish to check for equality then == is just fine. Also The function can help in sorting. To be more clear about sorting. Strcmp() returns less than 0 if string1 sorts before string2, greater than 0 if string2 sorts before string1 or 0 if they are the same. For example

$first_string = "aabo";$second_string = "aaao";echo $n = strcmp($first_string,$second_string);The function will return greater than zero, as aaao is sorting before aabo.

Share Tweet Linkedin Pinterest
Previous Post

Php Foreach Loop To Iterate Over Elements Of An Array

Next Post

Làm thế nào để bật một hộp thông báo cảnh báo bằng php?

CÙNG CHUYÊN MỤC

form register php

Form register php

09/04/2021
fantastic blog (cms) in php with source code

Fantastic blog (cms) in php with source code

28/04/2021
validate form php javascript

Validate form php javascript

28/04/2021
http diendanlequydon com viewtopic php style 6

Http diendanlequydon com viewtopic php style 6

28/04/2021
cách làm website bằng php

Cách làm website bằng php

25/05/2022
hướng dẫn cài đặt và sử dụng composer trên ubuntu 20

Hướng dẫn cài đặt và sử dụng composer trên ubuntu 20

25/05/2022
so sánh giờ trong php

So sánh giờ trong php

25/05/2022
hướng dẫn sử dụng chức năng

Hướng dẫn sử dụng chức năng "config php timezone"

24/05/2022

Newsletter

The most important automotive news and events of the day

We won't spam you. Pinky swear.

Chuyên Mục

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ

News Post

  • Nintendo ds (nds) roms

About

Chúng tôi tạo ra trang web nhằm mục đích mang lại kiến thức bổ ích cho cộng đồng, các bài viết được sưu tầm từ nhiều nguồn trên internet giúp mang lại kiến thức khách quan dành cho bạn

©2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng

Liên Hệ - Giới Thiệu - Nội Quy - Bảo Mật

No Result
View All Result
  • Trang chủ
  • Chuyên mục
    • Hỏi Đáp
    • Kiến Thức
    • Sức Khỏe
    • Tử Vi
    • Công Nghệ
  • Lưu trữ
  • Liên hệ

© 2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng.