- PHP Enhanced Snippets — VScode extension
- Usage
- Design Principles
- Snippets
- Shortcuts
- PHP Tag
- Branching
- Loop
- Properties
- Functions
- Methods
- Class related
- Error
- Special Thanks
- PHP in Visual Studio Code
- Linting
- Windows
- Linux and macOS
- Snippets
- PHP extensions
- Disable built-in PHP support
- Debugging
- Next steps
- PHP snippets
- Import and export
- Issues & Suggestions
PHP Enhanced Snippets — VScode extension
This VScode extension provides a complete set of code snippets for PHP developers. You can use it to avoid wasting time typing Class blocks, function signatures or other common PHP statements.
Usage
- ‘php class’ = ph cl / php cla (no spaces when typing)
- ‘php class with implements’ = ph cl im / php cla imp
Design Principles
- No unnecessary placeholders.
- ❌ if (condition) else
- ✅ if () else <>
- ❌ function-public
- ✅ public-function
- ❌ wl
- ✅ while-block
Snippets
Shortcuts
Snippet Output Language r / re return php th $this-> php se self:: php thp $this->property = $property; php pr print_r() php vd var_dump() php dirname-dir dirname(__DIR__) php PHP Tag
Snippet Output Language php html php-open html php-close ?> html php-page php page with strict type & namespace html Branching
Snippet Output Language if-block if ($condition) <> php if-else-block if ($condition) <> else <> php if-return-block if ($condition) < return $foo; >return $bar; php else-block else <> php elseif-block elseif ($condition) <> php switch-break switch() php switch-return switch() php if-three / ternary-operator (condition)?true:false; php Loop
Snippet Output Language while-block while ($condition) <> php do-while-block do <> while ($condition); php for-i for ($i = 0; $i php for-j for ($j = 0; $j php foreach-block foreach ($array as $value) <> php foreach-key-value foreach ($array as $key => $value) <> php Properties
Snippet Output Language public-bool
^(protected | private) ~public bool $ php public-int
^(protected | private) ~public int $ php public-float
^(protected | private) ~public float $ php public-string
^(protected | private) ~public string $ php public-array
^(protected | private) ~public array $ php public-readonly-bool
^(protected | private) ~public readonly bool $ php public-readonly-int
^(protected | private) ~public readonly int $ php public-readonly-float
^(protected | private) ~public readonly float $ php public-readonly-string
^(protected | private) ~public readonly string $ php public-readonly-array
^(protected | private) ~public readonly array $ php Functions
Snippet Output Language function function name($param) <> php function-return function name($param): string <> php anonymous-function function ($param) <> php anonymous-function-return function ($param): string <> php anonymous-function-use function ($param) use ($var) <> php anonymous-function-use-return function ($param) use ($var): string <> php arrow-function fn($foo) => $bar; php arrow-function-return fn($foo): string => $bar; php arrow-function-nested fn($foo) => fn($bar) => $baz; php Methods
Snippet Output Language public-construct
^(protected | private) ~public function __construct() php public-function
^(protected | private) ~public function php public-function-return
^(protected | private) ~public function with return type php public-static-function
^(protected | private) ~public static function php public-static-function-return
^(protected | private) ~public static function with return type php abstract-public-function
^protected ~abstract public function php abstract-public-function-return
^protected ~abstract public function with return type php Class related
Snippet Output Language php-class php class html, php php-class-no php class without strict type & namespace html php-class-no-strict php class without strict type html php-class-no-namespace php class without namespace html php-class-extends php class with extends html, php php-class-implements php class with implements html, php php-abstract-class php abstract class html, php php-abstract-class-no php abstract class without strict type & namespace html php-abstract-class-no-strict php abstract class without strict type html php-abstract-class-no-namespace php abstract class without namespace html php-abstract-class-extends php abstract class with extends html, php php-abstract-class-implements php abstract class with implements html, php php-final-class php final class html, php php-final-class-no php final class without strict type & namespace html php-final-class-no-strict php final class without strict type html php-final-class-no-namespace php final class without namespace html php-final-class-extends php final class with extends html, php php-final-class-implements php final class with implements html, php php-interface php interface html, php php-interface-no php interface without strict type & namespace html php-interface-no-strict php interface without strict type html php-interface-no-namespace php interface without namespace html php-interface-extends php interface with extends html, php php-trait php trait html, php Error
Snippet Output Language thr throw new \Exception() php try-catch-block try<> catch ()<> php try-catch-message try<> catch (Exception $e)< $e->getMessage() > php try-catch-finally try<> catch ()<> finally <> php catch-block catch ()<> php finally-block finally <> php Special Thanks
This extension is forked from h4kst3r/php-awesome-snippets. Because its author seems to have not maintained it for a long time. So I made this fork to make the extension suitable for new version of PHP. I reorganized the snippets and added more standardized writing. Great thanks to h4kst3r’s excellent work.
The new version number starts from 2.x.
This work is inspired by PHPstorm (PHP Live Templates) and other works available on VScode marketplace like PHP Snippets VS Code or PHP Snippet Pack.
PHP in Visual Studio Code
Visual Studio Code is a great editor for PHP development. You get features like syntax highlighting and bracket matching, IntelliSense (code completion), and snippets out of the box and you can add more functionality through community-created VS Code extensions.
Linting
VS Code uses the official PHP linter ( php -l ) for PHP language diagnostics. This allows VS Code to stay current with PHP linter improvements.
Tip: Using XAMPP? Install the full version of PHP in order to obtain the development libraries.
There are three settings to control the PHP linter:
- php.validate.enable : controls whether to enable PHP linting at all. Enabled by default.
- php.validate.executablePath : points to the PHP executable on disk. Set this if the PHP executable is not on the system path.
- php.validate.run : controls whether the validation is triggered on save (value: «onSave» ) or on type (value: «onType» ). Default is on save.
To change the PHP settings, open your User or Workspace Settings ( ⌘, (Windows, Linux Ctrl+, ) ) and type ‘php’ to filter the list of available settings.
To set the PHP executable path, select the Edit in settings.json link under PHP > Validate: Executable Path, which will open your user settings.json file. Add the php.validate.executablePath setting with the path to your PHP installation:
Windows
"php.validate.executablePath": "c:/php/php.exe" >
Linux and macOS
"php.validate.executablePath": "/usr/bin/php" > or "php.validate.executablePath": "/usr/local/bin/php" >
Snippets
Visual Studio Code includes a set of common snippets for PHP. To access these, hit ⌃Space (Windows, Linux Ctrl+Space ) to get a context-specific list.
PHP extensions
There are many PHP language extensions available on the VS Code Marketplace and more are being created. You can search for PHP extensions from within VS Code in the Extensions view ( ⇧⌘X (Windows, Linux Ctrl+Shift+X ) ) then filter the extensions dropdown list by typing ‘php’.
Disable built-in PHP support
To disable the built-in PHP smart completions in favor of suggestions from an installed PHP extension, uncheck PHP > Suggest: Basic, which sets php.suggest.basic to false in your settings.json file.
Debugging
PHP debugging with XDebug is supported through a PHP Debug extension. Follow the extension’s instructions for configuring XDebug to work with VS Code.
Next steps
Read on to find out about:
PHP snippets
Snippets are optimized to be short and easy to remember.
Below is a list of all available snippets and the triggers of each one. The ⇥ means the TAB key.
Import and export
Trigger Content eco⇥ echo «»; statement. pri⇥ print($ex); statement. dump⇥ var_dump($ex) statement pre_dump⇥ echo ‘ ';\nvardump($ex); \necho '
‘; formated var_dump output
fore⇥ foreach(iterable_expr as $value) forek⇥ foreach(iterable_expr as $key => $value) prif⇥ private function name() <> prisf⇥ private static function name() <> prof⇥ protected function name() <> prosf⇥ protected static function name() <> pubf⇥ public function name() <> pubsf⇥ public static function name() <> fn⇥ fn($x) => $x * 10 Arrow function inc⇥ include $file include filename inco⇥ include_one $file include_one filename req⇥ require $file require filename reqo⇥ require_once $file require_once filename thr⇥ throw new $0 throw an exception ns⇥ namespace App; namespace App is an example mt⇥ match($ex) match expression G⇥ $_GET[‘var’] $_GET Superglobal P⇥ $_POST[‘var’] $_POST Superglobal SE⇥ $_SERVER[‘var’] $_SERVER Superglobal SESS⇥ $_SESSION[‘var’] $_SESSION Superglobal REQ⇥ $_REQUEST[‘var’] $_REQUEST Superglobal FI⇥ $_FILE[‘var’][‘name’] $_FILE Superglobal Issues & Suggestions
For any issues or suggestions, please use GitHub issues.
Happy coding _ PHP developer _