2

I've been working with a file upload script written in PHP. But I can't find any documentation on the "or error()" function used.

Is this a php error function? Or a function of the file handling library? Trying to find documentation so I can make the necessary changes, and I am not coming up with anything in the PHP documentation.

Here is the relevant code segment:

 $errors = array(1 => 'php.ini max file size exceeded', 
                    2 => 'html form max file size exceeded', 
                    3 => 'file upload was only partial', 
                    4 => 'no file was attached');

($_FILES[$fieldname]['error'] == 0) or error($errors[$_FILES[$fieldname]['error']], $uploadForm);
    @is_uploaded_file($_FILES[$fieldname]['tmp_name'])
    or error('not an HTTP upload', $uploadForm);
8
  • there is no such thing as an or in PHP. it is represented with || while && for and. i do believe those should be inside an if statement. Commented May 30, 2017 at 5:08
  • 1
    @hungrykoala Yes there is. You can write it as or. Commented May 30, 2017 at 5:09
  • @ChrisBurton oh, my mistake then. but I still believe those should be inside an if statement. it doesn't make sense to write those lines like that. Commented May 30, 2017 at 5:10
  • 1
    @hungrykoala Logical operators are not explicitly for conditionals. Commented May 30, 2017 at 5:10
  • @ChrisBurton true but if left like that the first statement doesn't seem to do anything. unless you assign that into a variable. the second statement is plausible as it is used in as a function parameter though the error is suppressed. Commented May 30, 2017 at 5:14

2 Answers 2

2

error is not a function defined in PHP, it's some custom function.
If this is sample code, it's probably supposed to mean "fill in your error handling logic here."


foo() or bar();

This pattern is used as a replacement for if (!foo()) bar(); here. or is the same as the || logical operator (except it has lower precedence, which we'll ignore here). It has the same characteristic as || in that it is short-circuiting: the right hand side will only be evaluated if the left hand side is falsey. So if foo() is "successful", bar() won't be executed. In concrete terms this means that error() is called if the left hand side expression "fails".

Using or instead of if is a stylistic choice; IMO it's a bad choice, and the fact that it produces questions like this is enough evidence for that.

Sign up to request clarification or add additional context in comments.

Comments

0

error() is not a native PHP function. It's probably just a custom function that adds nicer messages.

Here are the error codes that are elaborated in the $errors array: http://www.php.net/manual/en/features.file-upload.errors.php

2 Comments

So, if I haven't defined this explicitly anywhere; then it just isn't doing anything? I had assumed it was a native function and never given it another thought. The script didn't come with a library and I don't believe it is based on any includes either.
Hmm, I found some pages with that script: stackoverflow.com/questions/18544357/… for example. You should avoid copy-pasting code that you don't understand. If anything, use code examples from the (comments) on PHP.net's manual pages. The community is pretty active and upvotes usually point you to good code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.