If statement

Hello i have an if statement which outputs text if UserName=Test but i found out the if statement is case sensitive. How can we change the statement to turn off case sensitive?

try

 
if(strtolower($UserName) == 'test')


if (strtoupper($username) == "TEST") {
...
}

haha… nice one Kalon

You could normalise the strings before comparison, or use [fphp]strcasecmp[/fphp].


<?php
if(0 === strcasecmp('Anthony', 'anthony')){
  #same
}

var_dump(
  0 === strcasecmp('Anthony', 'anthony')
); #true

var_dump(
  0 === strcasecmp('anthony', 'anthony')
); #true
?>

the thing is, the script is a private message script to see if the message is allowed to see to the user who it is to, i have over 1600 messages with usernames at different capital outputs. How could i solve this?

to see if the message is allowed to see to the user who it is to,

not sure what this means :weyes:

i have a table named privatemessages with already over 1600 messages stored with a field named touser. When i read a message on my site it uses an if statement to see if you are logged in as the user who the message is to.

I cannot use your codes above because i already have over 1600 messages stored with the touser entrys with different capital letters. Do we have a code to bypass it?

This is v confusing… are you saying you have 1600 if statements?

can you post the current IF statement

Oh my. :smiley:

SELECT id, title, content FROM private_message WHERE recipient LIKE 'username'

$query2 = mysql_query("SELECT * FROM chatmail WHERE MessageID='$search' ")
or die("Could not insert data because ".mysql_error());
$qry2 = mysql_fetch_array( $query2 );

if ($myuser == "$qry2[ToUser]")
{


$query2 = mysql_query("SELECT * FROM chatmail WHERE MessageID='$search' ")
or die("Could not insert data because ".mysql_error());
$qry2 = mysql_fetch_array( $query2 );

if (strtoupper($myuser) == strtoupper($qry2[ToUser]))
{

You could also use AnthonySterling’s string comparison… or even integrate the if into the query


$query2 = mysql_query("SELECT * FROM chatmail WHERE MessageID='$search'  AND ToUser like '$myuser'")

Do you really want to return all messages from the database when you only need a couple? Do you think PHP should do this level of data filtering? :wink:

I doubt this if was working as posted.

I think you would at least need $qry2['ToUser])

and to make it case insensitive

 
if(strtolower($myuser) == strtolower($qry2['ToUser'])) {

it doesnt seem to like it in my php if i put a ’ ’ between the variables:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

hey guys i managed to alter it to get it working, thanks.

This topic is a perfect example of how you can misuse the PHP AND design your application in the worst possible way.
What’s sad, it attracts users :frowning:

I don’t want to bash on the OP here, but it is true that the whole design of the feature is horrible.
Why?

The OP ran into a problem of checking two or more database entries. He goes on to do it by sequence of characters (in this example - usernames).
Isn’t it more logical to use NUMBERS for comparison rather than usernames?
I mean, it’s so much easier for the computer to do it and for human to understand the logic behind it.
I won’t bother to mention absolutely terrible named arrays and unquoted associative indexes. I also understand that there are people new to the programming and application design, but I am baffled how they never wonder whether there’s a better way if they stumple upon a wall they can’t cross.

I’m sorry if anyone got offended by this post, it wasn’t the intention.

Not their fault. Good PHP tutorials are buried pretty well. PHP has great documentation in www.php.net, but its tutorials beyond initial syntax are lacking.

No to hijack the thread, but I think this is due to the fact that PHP is very versatile and therefore, even among the top PHP developers, implementation varies dramatically and there is no ‘standard’.