4

when i save my php file then VS Code removes ?> tag from ending. and its causing problems in execution. I dont know why is this happening. If anyone knows please help. Thank u.

Update: I was using Es-lint and Prettier extensions at the same time. I removed them and now its fine.

JoharKhan
  • 226
  • 4
  • 7
  • 8
    It's apparently good practice to remove it. PHPStorm even raises warnings if you have a closing tag in your PHP. I wouldn't be surprised if VS Code (or whatever plugin you're using) is removing it for style reasons. Are you absolutely sure it's the removal of your closing tag that's causing problems? – Carcigenicate Oct 01 '20 at 18:38
  • 2
    It is probably related to code style auto-formatting. Check to see if it is set to use PSR-1, PSR-2 and/or PSR-12. The reason it is considered good practice is that whitespace after this tag can be sent to the output buffer, causing issues with things like `header();` – Alex Barker Oct 01 '20 at 18:40
  • 3
    There's no reason this should cause execution issues. Skipping it is actually recommended; it prevents an accidental space after it from breaking sites. – ceejayoz Oct 01 '20 at 18:43
  • @Carcigenicate ur right i was using some extensions and those caused me some problems. btw extensions were es-lint and prettier – JoharKhan Oct 02 '20 at 10:50

2 Answers2

1

I find two ways to solve this problem.

  1. Go to VSCode settings and in the search bar type "format", then uncheck Editor: Format on save. Formatting the code will no longer work, but VSCode will also not delete the PHP closing tag.

  2. Install the php cs fixer extension and I hope it will solve the issue.

Vukasin
  • 36
  • 5
0

End tag removal is covered here in detail: PHP end tag "?>" It's recommended practice for pure PHP files.

Ahmad
  • 116
  • 1
  • 8