Jump to content

| and || returning unexpected results...


freelance84

Recommended Posts

$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?

 

Link to comment
Share on other sites

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/";
						}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.