Unexpected token if in php

I'm trying to get a directory made, after which a file is uploaded, the code looks a bit like this:

    login($inputs ['email'], $inputs ['password']) &&
      mkdir('file/file/'.$_SESSION['user_id'].'/Profile', 0777, true) &&
      logout() &&
(if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
  echo "Sorry, there was an error uploading your file.";
})

However, if i use this snippit, i get the error code "Parse error: syntax error, unexpected token "if"", if i replace the "&&" with a semi-colon, the upload script will not execute! How do i make this work?

asked Feb 15 at 14:52

Unexpected token if in php

4

The code you provided doesn't seem to be complete, but to get the question answered: You don't have to write "if" inside another "if" condition.

// this
if (condition1 == true) {
  if (condition2 == true) {}
}
// is the same as
if (condition1 == true && condition2 == true) {}

So the following is not needed and will most likely end in some kind of parsing error.

// the second "if" is not valid, you can keep the (), though
if ((condition1 == true) && if(condition2 == true)) {}

In order to clean this up you can of course nest these statements in functions, as you did with logout() and call these functions and chain them with && or || operators.

function folderIsCreated() {
  return mkdir(...);
}
// function 2...
if (folderIsCreated() && function2() && function3()) {}

Long story short for your code: Remove the if inside the if.

answered Feb 15 at 15:30

clashclash

3773 silver badges8 bronze badges

1

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

What is unexpected token if?

Like other programming languages, JavaScript has define some proper programming rules. Not follow them throws an error.An unexpected token occurs if JavaScript code has a missing or extra character { like, ) + – var if-else var etc}. Unexpected token is similar to syntax error but more specific.

What is unexpected token in PHP?

Unexpected – This means the code is missing a character and PHP reaches the end of the file without finding what it's looking for. The error will include information at the end that explains what it saw that was unexpected.

How do I fix unexpected token error?

This error can occur for a number of reasons, but is typically caused by a typo or incorrect code. Luckily, the SyntaxError: Unexpected token error is relatively easy to fix. In most cases, the error can be resolved by checking the code for accuracy and correcting any mistakes.

How do you fix syntax error near unexpected token then?

One Approach to Fix Them All.
Run the script that contains the syntax error..
Take note of the line mentioned by the Bash error..
Execute the line with the error in a Bash shell to find the error fast (without having to change the script and rerun it multiple times)..
Update your script with the correct line of code..