0

I'm writing a relatively simple if statement to process the response of a web-service

The below code will output the type of data that's being returned and the value of it:

$response = $client->__getLastResponse();
echo $response;
echo gettype($response);

For a correct response, I get 'true string'
For an incorrect response, I get 'false string'

So I need an if statement to process whether it's true or false:

if($response == "true"){
    echo "Logged In";
} else {
    echo "Not Logged In";
}

Whatever response I get, I always receive "Not Logged In"

I've tried the === operator also, but to no avail.

Can someone help?

4
  • If you get "true string" it means the response variable contains "true " (with a final space), not "true". Also, var_dump($response) is a cleaner way of displaying values and types - can you tell us what it outputs ? Commented Jan 12, 2012 at 12:52
  • string(536) " true " which indeed means there's a space. Commented Jan 12, 2012 at 13:03
  • I have already answered. Is it really true string you get or just true as a string? Commented Jan 12, 2012 at 13:03
  • The error was that the response from the webservice (not of my own design I might add) returned 536 characters to display the word true Commented Jan 12, 2012 at 15:12

1 Answer 1

2

For a correct response, I get 'true string' For an incorrect response, I get 'false string'

Provided that's you actual code, you have echo $response . gettype( $response ) which yields "true string", right? If so, there's a space after "true". Try to trim the response:

<?php
$response = $client->__getLastResponse();
if( strtolower( trim( $response ) ) === 'true' ) {
   echo 'response is true.';
}

Apart from that, debugging is really easy using var_dump, as that will display quotes around the value and the total length of the string, making the additional space pop-up much easier to spot.

Sign up to request clarification or add additional context in comments.

7 Comments

Your excessive whitespace usage is pretty ugly.
@ThiefMaster Aww, thanks for taking the time to share that with me. I find it to be more readable, actually.
For guy like me, its excessive. I use no space unless I have to. see code example in my answer
Interesting, I usually consider it pretty hard to read with that many spaces since things that belong together are separated... already hate it in the jquery sourcecode.
@ThiefMaster Well, what can I say? Coding styles are personal, and this happens to be mine. You could argue it's not what most developers use, but then, my IDE can reformat ;)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.