0

I have these declared

$username = $this->users->getUsernameById($file->user);
$sessionuser = $this->session->userdata('username');

How would I go about comparing to the two values and if they match, execute code? I had

if ( $username == $sessionuser){

blalghalhalhalhllahblahhblahhblahhh
}

However, even when the two variables are the same, it doesn't seem to trigger the if statement. Did I do something wrong? maybe not ==, just =?

2 Answers 2

2

Use strcmp() function - or strcasecmp() for case-insensitive comparison.

if ( strcmp($username, $sessionuser) == 0) {
Sign up to request clarification or add additional context in comments.

Comments

2

Generally speaking PHP is very forgiving when it comes to comparison operations because of the loosely typed dynamic nature of the language.

Try this to debug your code:

echo $username . '<br/>' . $sessionuser;

You may find that the values you are expecting aren't actually the values you're getting.

Another thing that I frequently have to do when stepping through my code is to place something like this:

echo 'got here';
exit;

immediately inside the if block to very whether it is that the block is not executing or whether it is the code within the block that is not executing as I expected it to.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.