Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/250786-string-comparison-failing/
Share on other sites

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.

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.

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.

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.

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.

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.

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?

-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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.