Rheves Posted August 16, 2012 Share Posted August 16, 2012 I'm working on a file upload script and have the following code: if ($_FILES["filename"]["error"] > 0){ if ($_FILES["filename"]["error"] == 1){ $FileCheck = "Failure, file size too large."; } echo "Error: " . $_FILES["filename"]["error"] . "<br />"; return false; }else{ //Do upload return true; } The line checking that error = 1 never evaluates to true, but the following echo statement spits out 1. I tried doing a "$_FILES["filename"]["error"] * 1" to force it to be an int but that didn't help. This seems very simple but I can't figure out why it isn't comparing properly? Quote Link to comment https://forums.phpfreaks.com/topic/267191-php-int-compare-problem/ Share on other sites More sharing options...
xyph Posted August 16, 2012 Share Posted August 16, 2012 You haven't checked if it returns true in your posted code. Quote Link to comment https://forums.phpfreaks.com/topic/267191-php-int-compare-problem/#findComment-1369968 Share on other sites More sharing options...
Rheves Posted August 16, 2012 Author Share Posted August 16, 2012 I was checking the value of $FileCheck afterwards to check if the code had been executed, but it was always empty. I threw in some echoes and my mistake that code is being evaluated properly, but $FileCheck isn't being set properly. My code that calls the function is as follows and $FileCheck is always empty: if (SendIt()){//The function above //Worked $MessageToSend .= "\n Uploaded File: "(URL)/" . $_FILES["filename"]["name"]; $FileCheck = "<tr><td>File Upload:</td> <td>Success!</td></tr>"; }else{ //Didn't echo "send it returned false, FileCheck is: " . $FileCheck; } Quote Link to comment https://forums.phpfreaks.com/topic/267191-php-int-compare-problem/#findComment-1369971 Share on other sites More sharing options...
KevinM1 Posted August 16, 2012 Share Posted August 16, 2012 Take a look at your quotes.... Quote Link to comment https://forums.phpfreaks.com/topic/267191-php-int-compare-problem/#findComment-1369973 Share on other sites More sharing options...
xyph Posted August 16, 2012 Share Posted August 16, 2012 Nvm, misread. Quote Link to comment https://forums.phpfreaks.com/topic/267191-php-int-compare-problem/#findComment-1369974 Share on other sites More sharing options...
Rheves Posted August 16, 2012 Author Share Posted August 16, 2012 Sorry, they are valid in my script, I was just removing the actual url being used: if (SendIt()){//The function above //Worked $MessageToSend .= "\n Uploaded File: (URL)/" . $_FILES["filename"]["name"]; $FileCheck = "<tr><td>File Upload:</td> <td>Success!</td></tr>"; }else{ //Didn't echo "send it returned false, FileCheck is: " . $FileCheck; } Is what it should be, the problem isn't there. Quote Link to comment https://forums.phpfreaks.com/topic/267191-php-int-compare-problem/#findComment-1369975 Share on other sites More sharing options...
Rheves Posted August 16, 2012 Author Share Posted August 16, 2012 nevermind, misread. I saw your post before you edited it and passing it by reference fixed it, even though I had $FileCheck declared at the very top of my script, Though I am now getting the following error: PHP Warning: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of SendIt(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file in [...] on line 137 But I can work from there, thank you! I'll try to post more of my source code if I need any help in the future too, I kind of made a mess by only posting a little bit. Edit: The above was caused by my putting the & in front of where I called SendIt instead of where I declare the function. Quote Link to comment https://forums.phpfreaks.com/topic/267191-php-int-compare-problem/#findComment-1369979 Share on other sites More sharing options...
xyph Posted August 16, 2012 Share Posted August 16, 2012 Okay, so I was understanding it properly. Wish I could take back that edit You have to change how you've declared your function. You have to add the argument when you declare the function <?php function testIt(&$message) { // Some test that could be true or false if( rand(0,1) ) { $message = 'It was successful'; return true; } else { $message = 'It failed'; return false; } } if(testIt($response)) { echo 'Function returned true, message was: ' . $response; } else { echo 'Function returned false, message was: ' . $response; } ?> function testIt(&$message) Please, show us how you're using it, and how you've declared the SendIt function Quote Link to comment https://forums.phpfreaks.com/topic/267191-php-int-compare-problem/#findComment-1369982 Share on other sites More sharing options...
Rheves Posted August 16, 2012 Author Share Posted August 16, 2012 I was getting that error when I called it like this: if (SendIt(&$FileCheck)){ //Worked }else{ //Didn't } But removing that & and changing the declare function to read this instead: function SendIt(&$EM){ [...] }//I changed it to EM within the function Fixed it. Quote Link to comment https://forums.phpfreaks.com/topic/267191-php-int-compare-problem/#findComment-1369985 Share on other sites More sharing options...
xyph Posted August 16, 2012 Share Posted August 16, 2012 Perfect! Don't forget to read up on variable scope! It's important to know how it works when developing. Keep in mind some languages treat scope slightly differently, but the basic concepts are always the same. Quote Link to comment https://forums.phpfreaks.com/topic/267191-php-int-compare-problem/#findComment-1369987 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.