freelance84 Posted June 8, 2011 Share Posted June 8, 2011 $fileExt = 'pjpeg'; if($fileExt != ('jpeg'|'jpg'|'png'|'gif'|'pjpeg')) { echo 'boo'; } The above code returns 'boo'.... but the following does not. And both examples did not return any syntax errors. $fileExt = 'pjpeg'; if($fileExt != ('jpeg'||'jpg'||'png'||'gif'||'pjpeg')) { echo 'boo'; } I was expecting the result the 2nd example returned, but no the top... Could anyone tell me why? Is it something to do with regex? Quote Link to comment https://forums.phpfreaks.com/topic/238783-and-returning-unexpected-results/ Share on other sites More sharing options...
Pikachu2000 Posted June 8, 2011 Share Posted June 8, 2011 Wrong syntax. You might want to consider using in_array for this anyhow. if( $fileExt != 'jpeg' && $fileExt != 'jpg' && $fileExt !='png' && $fileExt != 'gif' && $fileExt != 'pjpeg' ) Quote Link to comment https://forums.phpfreaks.com/topic/238783-and-returning-unexpected-results/#findComment-1226949 Share on other sites More sharing options...
fugix Posted June 8, 2011 Share Posted June 8, 2011 I would recommend using an array as well. something like $fileExt = "pjpeg"; $ext_arr = array("jpeg", "jpg", "png", "gif", "pjpeg"); if(!in_array($fileExt, $ext_arr)) { echo 'boo'; } Quote Link to comment https://forums.phpfreaks.com/topic/238783-and-returning-unexpected-results/#findComment-1226962 Share on other sites More sharing options...
freelance84 Posted June 8, 2011 Author Share Posted June 8, 2011 ahhh... so if( $fileExt != 'jpeg' && $fileExt != 'jpg' && $fileExt !='png' && $fileExt != 'gif' && $fileExt != 'pjpeg' ) is the same as if($fileExt != ('jpeg'|'jpg'|'png'|'gif'|'pjpeg')) in_array just seemed a little much for its application so was looking for a shorter way.... like so... (any thoughts?) $fileType = $_FILES['fileToUpload']['type']; $acceptableImageTypes = array('image/jpeg','image/jpg','image/pjpeg','image/gif','image/png'); if(in_array($fileType,$acceptableImageTypes)) { //the file name in the temp folder on the server: $theFile = $_FILES['fileToUpload']['tmp_name']; //check is image if(getimagesize($theFile)) { //get just the file ext $fileExt = str_replace('image/','',$fileType); //set the location for the said image if($fileExt == ('pjpeg'||'jpeg'||'jpg')) /////////////////here is the || instead of the in_array. using in array would require me to set another array. { $fileExt = 'jpeg'; //the new destination $destination = "../create/upload_container/jpeg/"; } elseif($fileExt == 'gif') { //the new destination $destination = "../create/upload_container/gif/"; } else{ //the new destination $destination = "../create/upload_container/png/"; } Quote Link to comment https://forums.phpfreaks.com/topic/238783-and-returning-unexpected-results/#findComment-1226973 Share on other sites More sharing options...
KevinM1 Posted June 8, 2011 Share Posted June 8, 2011 ahhh... so if( $fileExt != 'jpeg' && $fileExt != 'jpg' && $fileExt !='png' && $fileExt != 'gif' && $fileExt != 'pjpeg' ) is the same as if($fileExt != ('jpeg'|'jpg'|'png'|'gif'|'pjpeg')) Right, except the second one is not valid PHP and will not work. Quote Link to comment https://forums.phpfreaks.com/topic/238783-and-returning-unexpected-results/#findComment-1226980 Share on other sites More sharing options...
freelance84 Posted June 8, 2011 Author Share Posted June 8, 2011 Right, except the second one is not valid PHP and will not work. What is the second one then as it doesn't return any syntax errors when i run it... Quote Link to comment https://forums.phpfreaks.com/topic/238783-and-returning-unexpected-results/#findComment-1226984 Share on other sites More sharing options...
revraz Posted June 8, 2011 Share Posted June 8, 2011 && is AND, || is OR, | is nothing Quote Link to comment https://forums.phpfreaks.com/topic/238783-and-returning-unexpected-results/#findComment-1226985 Share on other sites More sharing options...
jcbones Posted June 8, 2011 Share Posted June 8, 2011 if($fileExt == ('pjpeg'||'jpeg'||'jpg')) //invalid syntax. //but you could: if(in_array(strtolower($fileExt),array('pjpeg','jpeg','jpg'))) Quote Link to comment https://forums.phpfreaks.com/topic/238783-and-returning-unexpected-results/#findComment-1226987 Share on other sites More sharing options...
fugix Posted June 8, 2011 Share Posted June 8, 2011 in_array just seemed a little much for its application so was looking for a shorter way.... like so... (any thoughts?) $fileType = $_FILES['fileToUpload']['type']; $acceptableImageTypes = array('image/jpeg','image/jpg','image/pjpeg','image/gif','image/png'); if(in_array($fileType,$acceptableImageTypes)) { //the file name in the temp folder on the server: $theFile = $_FILES['fileToUpload']['tmp_name']; //check is image if(getimagesize($theFile)) { //get just the file ext $fileExt = str_replace('image/','',$fileType); //set the location for the said image if($fileExt == ('pjpeg'||'jpeg'||'jpg')) /////////////////here is the || instead of the in_array. using in array would require me to set another array. { $fileExt = 'jpeg'; //the new destination $destination = "../create/upload_container/jpeg/"; } elseif($fileExt == 'gif') { //the new destination $destination = "../create/upload_container/gif/"; } else{ //the new destination $destination = "../create/upload_container/png/"; } you state that in_array() is too much and you/re looking for a shorter way. then you use it in your code? also in the code you provided, you did not filter the image size. Quote Link to comment https://forums.phpfreaks.com/topic/238783-and-returning-unexpected-results/#findComment-1226990 Share on other sites More sharing options...
KevinM1 Posted June 8, 2011 Share Posted June 8, 2011 Right, except the second one is not valid PHP and will not work. What is the second one then as it doesn't return any syntax errors when i run it... It's not returning any errors because you're using bitwise OR rather than logical OR. | is for bitwise operations, || is for logical operations. So, the following: if($fileExt != ('jpeg'|'jpg'|'png'|'gif'|'pjpeg')) Is saying "If $fileExt does not equal ('jpeg bitwise OR'd with 'jpg', bitwise OR'd with 'png', bitwise OR'd with 'gif', bitwise OR'd with 'pjpeg'), do the following...." For what bitwise OR actually is, see: http://en.wikipedia.org/wiki/Bitwise_operation#OR Suffice it to say, your particular OR's do a particular thing, which isn't what you want them to do. Quote Link to comment https://forums.phpfreaks.com/topic/238783-and-returning-unexpected-results/#findComment-1226996 Share on other sites More sharing options...
AbraCadaver Posted June 8, 2011 Share Posted June 8, 2011 Right, except the second one is not valid PHP and will not work. What is the second one then as it doesn't return any syntax errors when i run it... It's valid, it just doesn't do what you want it to. | is a bitwise OR operator so when operating on those strings you get 'wgg': if($fileExt != ('jpeg'|'jpg'|'png'|'gif'|'pjpeg')) Evaluates to: if('pjpeg' != 'wgg') // which is true || is a logical OR operator, so when evaluating those strings you get true: if($fileExt != ('jpeg'||'jpg'|'png'||'gif'||'pjpeg')) Evaluates to: if('pjpeg' != true) // which is false because a non-false string is type cast to true This would be true (notice the 2 equal signs): if('pjpeg' !== true) // which is true because one arg is a string and one is a boolean Quote Link to comment https://forums.phpfreaks.com/topic/238783-and-returning-unexpected-results/#findComment-1227000 Share on other sites More sharing options...
PFMaBiSmAd Posted June 8, 2011 Share Posted June 8, 2011 Edit: see AbraCadaver's post above for the same info as here ^^^ Sorry to jump in but 'pjpeg'||'jpeg'||'jpg' and 'jpeg'|'jpg'|'png'|'gif'|'pjpeg' are valid (assuming you want to do what they are actually doing) but don't work in this code because they are logically (true/false) or'ing (in the case of the || operator) or bitwise (binary) or'ing (in the case of the | operator) the strings together and then testing if the result of the or'ed expression is not equal to the variable. Quote Link to comment https://forums.phpfreaks.com/topic/238783-and-returning-unexpected-results/#findComment-1227008 Share on other sites More sharing options...
freelance84 Posted June 8, 2011 Author Share Posted June 8, 2011 Edit: see AbraCadaver's post above for the same info as here ^^^ Sorry to jump in but 'pjpeg'||'jpeg'||'jpg' and 'jpeg'|'jpg'|'png'|'gif'|'pjpeg' are valid (assuming you want to do what they are actually doing) but don't work in this code because they are logically (true/false) or'ing (in the case of the || operator) or bitwise (binary) or'ing (in the case of the | operator) the strings together and then testing if the result of the or'ed expression is not equal to the variable. Brilliant. Thank you very much everyone. in_array just seemed a little much for its application so was looking for a shorter way.... like so... (any thoughts?) if($fileExt == ('pjpeg'||'jpeg'||'jpg')) /////////////////here is the || instead of the in_array. using in array would require me to set another array. you state that in_array() is too much and you're looking for a shorter way. then you use it in your code? also in the code you provided, you did not filter the image size. Aye, the bit i left in was my attempt..... fail. (also i had never seen or thought of putting the array within the in_array it self, i had always declared the array prior to the in_array function) Regarding the filtering of the image size you mentioned. By that do you mean i am letting all sizes through at the moment, or that I'm using the getimagesize to determine if the file is an image incorrectly? (i'm currently reading up on resizing the image at the server) Quote Link to comment https://forums.phpfreaks.com/topic/238783-and-returning-unexpected-results/#findComment-1227016 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.