Hướng dẫn unexpected token php

Lượt xem: 13135 Ngày tạo: 03/02/2021 Ngày cập nhật: 16/02/2021

Nội dung chính

  • Hiện tượng
  • Nguyên nhân
  • Cách khắc phục (Fix)
  • Not the answer you're looking for? Browse other questions tagged php or ask your own question.
  • What is unexpected token if?
  • What is unexpected token in PHP?
  • How do I fix unexpected token error?
  • How do you fix syntax error near unexpected token then?

Hiện tượng

  • Kịch bản Javascript trên trang HTML không hoạt động hoặc bị ngắt quãng giữa chừng
  • Javascript Console báo lỗi: Uncaught SyntaxError: Unexpected token '<'.

Hướng dẫn unexpected token php

Ảnh chụp Javascript Console báo lỗi Uncaught SyntaxError: Unexpected token '<'.

Nguyên nhân

  1. Nguyên nhân phổ biến nhất là do lỗi cú pháp trong 1 Statement (câu lệnh) Javascript nào đó với < là ký tự bắt đầu. Ví dụ:
           var a = 1 + 2 ; 
       < // <-- Lỗi
       var tween = gsap.to("#play", {
          duration: 4,
          x: 750,
          rotation: 360,
          ease: "none",
          paused: true
       });
  2. Khai báo External Javascript nhưng giá trị src của là URL lại lại dẫn đến 1 file KHÔNG PHẢI là Javascript (HTML, CSS, Ảnh, Video...).
    Ví dụ:
  3. File External Javascript chứa ngôn ngữ HTML. Ví dụ source code của scripts.js
    ...
    var a = document.getElementById("result");

    // <-- Lỗi
    console.log(a);
    ...
  4. Bên trong Internal Javascript chứa ngôn ngữ HTML. Ví dụ:

Cách khắc phục (Fix)

Bước 1: Xác định đúng nguyên nhân và vị trí xảy ra lỗi

Hầu hết, vị trí lỗi code Javascript đều được thể hiện trên Javascript Console của trình duyệt ở dạng đường link bên góc phải của dòng error log. Bạn có thể click vào đường link này → Developer Tool sẽ tự động chuyển sang Tab source và chỉ rõ vị trí Statement lỗi.

Ảnh minh hoạ xác định vị trí lỗi trên Javascript Console

Trong trường hợp Javascript Console báo lỗi ở vị trí số 1 (vị trí đầu tiên) của file HTML, nghĩa là bạn khai báo External Javascript nhưng giá trị src của là URL lại lại dẫn đến 1 file KHÔNG PHẢI là Javascript (HTML, CSS, Ảnh, Video...) (Nguyên nhân số 2 mà mình đã giải thích bên trên)

Ảnh minh hoạ trường hợp Javascript Console báo lỗi ở vị trí số 1 (vị trí đầu tiên) của file HTML

Bước 2: Tiến hành fix lỗi

Sau khi bạn đã xác định được vị trí Statement đang bị lỗi, hãy mở Source HTML (đối với lỗi trong Internal Javascript) hoặc Source Javascript (đôi với lỗi trong External Javascript) để sửa lại Statement đó cho đúng cú pháp.

Ảnh minh hoạ fix Javascript Statement đang bị lỗi

Nếu lỗi đến từ nguyên nhân số (2), bạn hãy kiểm tra lại URL của file External Javascript và thay bằng 1 URL dẫn đến đúng file Javascript mà bạn đang cần.

Và chúng ta cũng đã đi qua mục cuối cùng trong bài hướng dẫn rồi. Nếu bạn có bất kỳ thắc mắc nào, hay không thực hiện được bước nào, hãy để lại bình luận bên dưới nhé. Nếu bạn cảm thấy bài viết hay và hữu ích, hãy để lại 1 like, share, đánh giá để ủng hộ đội ngũ biên tập của hoccode.org nhé!

Chúc các bạn thực hiện thành công!

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

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged php or ask your own question.
  • What is unexpected token if?
  • What is unexpected token in PHP?
  • How do I fix unexpected token error?
  • How do you fix syntax error near unexpected token then?
    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

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..