yamahabob Posted November 9, 2011 Share Posted November 9, 2011 I'm compiling and running C++ applications from PHP. I'm using backticks to capture the output of the applications ran. The output is just some simple text. In my case "Miles per gallon = x" where x is just a number. I cannot for the life of me get the comparison to return true. For example $command1="./a.out1 <input1"; $a = `$command1`; $command2="./a.out2 <input2"; $b = `$command2`; if(strcmp($a,$b)==0){ ... } else{ // this is always executing ... } The two strings $a and $b are both "Miles per gallon = 10". I have checked both variables using var_dump(bin2hex($a)) and get dump1= string(42) "4d696c6573207065722067616c6c6f6e203d203130" dump2= string(42) "4d696c6573207065722067616c6c6f6e203d203130" Any idea on what's happening? Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/ Share on other sites More sharing options...
Pikachu2000 Posted November 9, 2011 Share Posted November 9, 2011 What do you get if you var_dump them without the bin2hex conversion? Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/#findComment-1286707 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2011 Share Posted November 9, 2011 And is the code you posted all in the same program 'scope'? Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/#findComment-1286716 Share on other sites More sharing options...
yamahabob Posted November 9, 2011 Author Share Posted November 9, 2011 Correction to the original post. That output was using bin2hex only. Using var_dump only, I get: string(20) "Miles per gallon = 5" string(20) "Miles per gallon = 5" Yes, all is in the same scope. I never make any function calls or anything like that. Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/#findComment-1286719 Share on other sites More sharing options...
Pikachu2000 Posted November 9, 2011 Share Posted November 9, 2011 The only thing I saw was a reference to strings being zero-terminated when being returned from a call via `backticks` or shell_exec(). See the third user note in the manual entry for strcmp and see if it's possible for that to be the issue. Although on the surface it seems unlikely, it's worth looking at. Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/#findComment-1286721 Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 This is really odd, considering <?php $a = 'Miles per gallon = 5'; $b = 'Miles per gallon = 5'; if( strcmp($a,$b) == 0 ) echo 'same'; else echo 'different'; ?> will output 'same' If that is indeed the var dump, something must be changing the values somewhere. Is that your COMPLETE code? It seems you are quite capable of debugging things yourself, so I'm just shooting in the dark here. I've never encountered an issue with strcmp, then again, I've never used it with returns from an external application (why that would matter, I'm not sure). Pikachu seems to be on the right track. Try triming your values. before comparing. Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/#findComment-1286724 Share on other sites More sharing options...
yamahabob Posted November 9, 2011 Author Share Posted November 9, 2011 No, this wasn't really my code at all. It was only the jist of my code. I am using trim() as well and have tried the code with and without it. This code below is almost my code exactly with useless lines removed. $do="/home/ctaylo86/www-home/tmp/test" . $userID . " < " . $myInputFile; $studentOutput = `$do`; $testNum_print=$i+1; echo "<h1><u>Your Output for Test " . $testNum_print ."</u></h1><br />"; echo $studentOutput . "<br />"; exec("rm " . $myFile); $do="/home/ctaylo86/www-home/tmp/testTeacher" . $userID . " < " . $myInputFile; $teacherOutput=`$do`; echo "<h1><u>Instructor's Output for Test " . $testNum_print . "</u></h1><br />"; echo $teacherOutput . "<br />"; $studentOutput=trim($studentOutput); $teacherOutput=trim($teacherOutput); if(strcmp($teacherOutput,$studentOuput)==0){ echo "<br />Your output matches! <br /><hr><br />"; } else{ echo "<br />Your output does not match the instructor's output. Try again.<br /><hr><br />"; echo var_dump($studentOutput) . "<br />"; echo var_dump($teacherOutput) . "<br />"; $student_correct_solution=0; } This gave me the output listed above. Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/#findComment-1286736 Share on other sites More sharing options...
Pikachu2000 Posted November 9, 2011 Share Posted November 9, 2011 Typo here 'Ouput' ---> if(strcmp($teacherOutput,$studentOuput)==0){ Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/#findComment-1286738 Share on other sites More sharing options...
yamahabob Posted November 9, 2011 Author Share Posted November 9, 2011 Oh wow... I'm speechless. I've been looking at this for a week. This is when I look at C and say thank you for telling me I didn't define that variable. :| Thanks for all the help. All works now. I'll retreat into silence now... Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/#findComment-1286743 Share on other sites More sharing options...
Pikachu2000 Posted November 9, 2011 Share Posted November 9, 2011 This is a good example of why people should always copy and paste the code that's causing the issue, and not type up a pseudo version of it. It also demonstrates the importance of having error_reporting set up to display everything it encounters while developing. This would generate an undefined variable warning, and be fixed in no time at all. Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/#findComment-1286746 Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 Oh wow... I'm speechless. I've been looking at this for a week. This is when I look at C and say thank you for telling me I didn't define that variable. :| Thanks for all the help. All works now. I'll retreat into silence now... For the record, PHP tells you that you haven't defined a variable. You've decided to not show that feature, or someone decided for you with default settings. In your development server, you should always have all errors reported or logged. This can be changed in the PHP.ini file error_reporting = -1 display_errors = 1 This can also be changed on a per-script basis using the code in my signature. This won't affect parser errors though, as the script will be aborted before any lines are actually executed. The absence of a variable/key will be reported as a 'Notice' type error. Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/#findComment-1286750 Share on other sites More sharing options...
yamahabob Posted November 9, 2011 Author Share Posted November 9, 2011 Duly noted. I had absolutely no idea about PHP error reporting. I don't use PHP often. I looked it up and saw this: error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); I don't have access to the PHP.ini file so can I just add this to any page I have PHP code in? Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/#findComment-1286751 Share on other sites More sharing options...
Pikachu2000 Posted November 9, 2011 Share Posted November 9, 2011 To do it on a per-script basis at runtime, you'd add this immediately after your opening <?php tag. error_reporting(-1); ini_set('display_errors', 'On'); Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/#findComment-1286753 Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 -1 is a better option IMO. It tells the PHP engine to report ALL errors, rather than whitelisting a specific few. Though new error constants probably won't show up, this saves you from having to worry about it if they do. This is based on using a development server though. In a production environment, none of my advice applies. Quote Link to comment https://forums.phpfreaks.com/topic/250786-string-comparison-failing/#findComment-1286754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.