strcmp vs equal

This is a discussion on strcmp vs equal within the PHP Language forums, part of the PHP Programming Forums category; Hi I noticed in some examples to the encrypt functions of the PHP manual a syntax was used for password ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-04-2004
Markus Ernst
 
Posts: n/a
Default strcmp vs equal

Hi

I noticed in some examples to the encrypt functions of the PHP manual a
syntax was used for password checks such as

if (strcmp($userpassword, md5($_POST['password'])) == 0) {
// do login
}

What is the advantage of this compared to

if ($userpassword == md5($_POST['password'])) {
// do login
}

?

--
Markus


Reply With Quote
  #2 (permalink)  
Old 10-04-2004
iuz
 
Posts: n/a
Default Re: strcmp vs equal

Markus Ernst wrote:

> Hi
>
> I noticed in some examples to the encrypt functions of the PHP manual a
> syntax was used for password checks such as
>
> if (strcmp($userpassword, md5($_POST['password'])) == 0) {
> // do login
> }
>
> What is the advantage of this compared to
>
> if ($userpassword == md5($_POST['password'])) {
> // do login
> }
>
> ?
>


it's the same thing..

--
www.iuz-lab.info
Reply With Quote
  #3 (permalink)  
Old 10-04-2004
Andy Hassall
 
Posts: n/a
Default Re: strcmp vs equal

On Mon, 4 Oct 2004 15:58:14 +0200, "Markus Ernst" <derernst@NO#SP#AMgmx.ch>
wrote:

>I noticed in some examples to the encrypt functions of the PHP manual a
>syntax was used for password checks such as
>
>if (strcmp($userpassword, md5($_POST['password'])) == 0) {
> // do login
>}
>
>What is the advantage of this compared to
>
>if ($userpassword == md5($_POST['password'])) {
> // do login
>}


None as far as I'm aware.

strcmp would be more familiar for people from a C background (where == would
compare the pointers, not the contents of the strings, and so would be wrong in
most cases).

Perl people might not use == on strings as string compare is 'eq' in Perl, so
they may lean towards strcmp, perhaps.

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Reply With Quote
  #4 (permalink)  
Old 10-05-2004
Markus Ernst
 
Posts: n/a
Default Re: strcmp vs equal

Thank you both for your answers!

--
Markus


Reply With Quote
  #5 (permalink)  
Old 10-06-2004
Chung Leong
 
Posts: n/a
Default Re: strcmp vs equal


"Markus Ernst" <derernst@NO#SP#AMgmx.ch> wrote in message
news:416156f8$0$8107$afc38c87@news.easynet.ch...
> Hi
>
> I noticed in some examples to the encrypt functions of the PHP manual a
> syntax was used for password checks such as
>
> if (strcmp($userpassword, md5($_POST['password'])) == 0) {
> // do login
> }
>
> What is the advantage of this compared to
>
> if ($userpassword == md5($_POST['password'])) {
> // do login
> }
>


Well, in theory, the use of strcmp() is a little safer because you're always
comparing two strings. If for some reason $userpassword is set to an
integer, the MD5 would get casted into an integer for the purpose of
comparison.

Example:

$userpassword = 0;
if($userpassword == md5("Chicken")) {
echo "Chicken";
}

The condition would evaluate to true because the hash starts with the letter
'a', which becomes 0 when it's converted to integer.



Reply With Quote
  #6 (permalink)  
Old 10-06-2004
Daniel Tryba
 
Posts: n/a
Default Re: strcmp vs equal

Chung Leong <chernyshevsky@hotmail.com> wrote:
> Well, in theory, the use of strcmp() is a little safer because you're always
> comparing two strings. If for some reason $userpassword is set to an
> integer, the MD5 would get casted into an integer for the purpose of
> comparison.


So wahts the difference between strcmp() and === :)

== should IMHO be used as little as possible, if one knows the types one
is comparing and these should match (like in most cases) === is the way
to go.

--

Daniel Tryba

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:52 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0