rick.emmet Posted December 20, 2011 Share Posted December 20, 2011 Hi Everyone, I'm testing out some functionality and can't get the results I'm expecting. On a page that will process uploaded data and photos, I call a function from one of my libraries that will count the number of photos uploaded. That function ( countPhotos), first determines if no photos were uploaded, and if that's the case, returns False. The library is an Included file and I've been reading the manual on Return and they say that if the Return comes from an Include file, control is returned to the file that calls the function. This is a bit confusing to me. Here's my script: // THIS IS THE FUNCTION CALLING "countPhotos" // Count the number of files uploaded $photo_num = countPhotos(); if (false) { echo "<div id='mainContent'> <span class='browserTable'> <br /> <p> You did not upload any photos.<br /> <a href='paid_bicycle_form.php'>BACK</a> </p> </span> </div>"; //Display bottom wrapper main_wrpr_bottom(); // display the footer do_html_footer(); exit; } else { // Continue - the count will be used in all the photo processing functions echo "The number of photos is ".$photo_num; } // THIS IS WITHIN AN INCLUDE LIBRARY FILE function countPhotos() { // How many photos have been uploaded? if ($_FILES['userfile']['error'][0] == 4 && $_FILES['userfile']['error'][1] == 4 && $_FILES['userfile']['error'][2] == 4 && $_FILES['userfile']['error'][3] == 4 && $_FILES['userfile']['error'][4] == 4) { return false; } else { $count_photos = 0; foreach($_FILES['userfile']['tmp_name'] as $key => $tmp_name) { if (is_uploaded_file($tmp_name)) { $count_photos++; } } return $count_photos; } } If I select five photos and click on the submit button, I get the failure notice, “You did not upload any photos”. This is exactly the opposite behavior expected. If I click on the submit button without uploading any files at all (no photos), I get a message, “The number of photos is 0". This comes from the portion of the function, that calls countPhotos, below the Else statement; and would only be expected to be output if at least one photo (or file) was uploaded. I guess what is confusing me so much is that if the function doesn't receive the False from countPhotos, why would it execute the portion of the script the outputs the message, “You did not upload any photos”. Does anybody know what I'm doing wrong here? I very much appreciate your input! Cheers, Rick Quote Link to comment https://forums.phpfreaks.com/topic/253518-false-false/ Share on other sites More sharing options...
trq Posted December 20, 2011 Share Posted December 20, 2011 Your not testing against $photo_num, your testing against false. Quote Link to comment https://forums.phpfreaks.com/topic/253518-false-false/#findComment-1299567 Share on other sites More sharing options...
rick.emmet Posted December 20, 2011 Author Share Posted December 20, 2011 Thank you, Thorpe, I tried the following code and got it to work: $photo_num = countPhotos(); if ($photo_num == false) { We didn't cover this in class, which seem very strange (we were trying to cover almost all of "PHP MySQL Web Development"). I'll be using this all the time, so I appreciate the input! Cheers, Ricki Quote Link to comment https://forums.phpfreaks.com/topic/253518-false-false/#findComment-1299872 Share on other sites More sharing options...
gizmola Posted December 20, 2011 Share Posted December 20, 2011 Most developers would simplify this to: $photo_num = countPhotos(); if (!$photo_num) { // false } You can also simplify it even further: if (!$photo_num = countPhotos()) { // false } You might want to read the php manual on boolean conversion: http://us.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting Quote Link to comment https://forums.phpfreaks.com/topic/253518-false-false/#findComment-1299876 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.