Joshua's Docs - PHP - Assorted Notes

Pulling In and Referencing Other Files

Include and Require Method Calls

WARNING: Avoid using relative paths whenever possible. A common approach (example) is to define constants / globals that hold absolute paths that can be shared across your environment, so you don't always have to do a lookup with dirname(__FILE__) all the time.

NOTE: For both include and require, parenthesis around file name are not required, nor recommended.

Include vs Require

Short answer: require will throw a fatal error (halt execution) if it can't load the file, whereas include will not.

Echoing out JavaScript, CSS, and other Text

If you have a local file, such as a JavaScript .js file, that you simply want to echo out into the page, you can use echo file_get_contents() or the readfile() function.

For example:

<?php echo file_get_contents($jsDirPath . '/myJsFile.js'); ?>

How do I...

  • Initialize an array:

    • Simple (numerical indexed): $myArr = array("alpha", "bravo");
    • Associative:

      $myArr = array(
      	"primaryColor" => "red",
      	"secondaryColor" => "blue"
      // Or, with v > 5.4
      $myArr = [
      	"primaryColor" => "red",
      	"secondaryColor" => "blue"
  • Add to an array

    • array_push($myArr, $valToPush);
  • Get the length of something

  • Use types

  • Use default values in function arguments

    • See Docs
    • General rules:

      • Default value must be constant value, not variable or function call
      • Default args / values should always come last
  • Safely check variables without throwing undefined errors?

    • empty() or isset() seem to be the only two ways to safely check for defined.
    • Things like gettype() or equality checks will emit errors if the variable is undefined
  • Suppress errors

    • It almost goes without saying that errors shouldn't be ignored; best option is try / catch, log properly, etc.
    • However, if you really do need to suppress, PHP has a call syntax - prefix command with @, which is an error control operator
  • Get a PHP REPL

    • PHP CLI, with -a flag for interactive shell (php -a) (docs)


The PHP manual has a good guide on objects in PHP.

Instantiating Objects in PHP

There are multiple options for how to instantiate an "object" in PHP.

  • Option: Create a class with public members, and call with new

    class MyClass {
    	public $primaryColor = 'red';
    $myObj = new MyClass();
  • Option: Cast an associative array to an object:

    $myObj = (object) array(
    	'primaryColor' => 'red'

For creating an empty object (for example, as a bucket for future values), you can use the methods above, but in a streamlined way:

  • You can use $emptyObj = new stdClass();
  • You can use $emptyObj = (object) [];


PHP, like some other languages out there, has a user-base that has been kind of slow to adopt VSCode as an IDE. As a bit of a self-fulfilling prophecy (why build something if no one is going to use it?), PHP support in VSCode is a little lackluster (at least at the moment).

In general, the PHP community tends to use software from the JetBrains company; particularly PhpStorm. To be fair to PHP devs, PhpStorm has been around since 2009, while VSCode has only been around since 2015.

WordPress Editing in VSCode

I recommend either:

  • Edit your theme / plugin by opening an entire Wordpress installation directory (e.i., the root folder that contains wp-config.php), so VSCode can pick up the function declarations

    • And / Or...
  • Install the WordPress Snippets extension

Formatting PHP in VSCode

One of the most popular extensions for PHP in VSCode is PHP Intelephense by Ben Mewburn. This can function as a formatter, but it has limited controls when it comes to that (for example, disabling Allman braces is not yet an option).

An alternative is phpfmt - PHP formatter. Although it is no longer maintained (😢), it is one of the few PHP formatting extensions, and it still works. For example, if you want to default to K&R style braces instead of Allman, you can use these settings.



XDebug is the best solution. Usually all it takes to enable is to download the matching DLL (use this tool to find matching version) and place in extension folder. Then add these lines to php.ini config file (example):


In VSCode, this is usually the config I use in launch.json for debugging:

	"name": "Listen for XDebug",
	"type": "php",
	"request": "launch",
	"port": 9000

Warning: Adding XDebug will usually add some additional load time in your local environment

For use in VSCode, you will also want to install the PHP Debug Adapter by Becker.

Manually triggering XDebug

Aside from adding breakpoints, you can also trigger your IDE debugger manually (similar to JavaScript debugger; kw), by using xdebug_break() (details). Just make sure to not have in production code!!!

Profiling with XDebug

Follow the official guide.

Sample php.ini settings:


For profile viewing, WinCacheGrind was easy to use on Windows.

Type Annotations / Comments

In general, support for type annotations through comments generally depends on the IDE you are using and/or installed extensions. However, The general accepted standard is PHPDoc, as opposed to alternatives. Here is the PHPDoc Reference Page.

Here are some extra tricks for using PHPDoc:

  • If you are using intelephense with VSCode, you can check out intelephense-docs
  • Although you cannot document things like stdclass inline, you can add PHP files that are strictly for annotating structures and don't actually do anything at run-time

As of right now (09/13/2020), there is no option to ignore single errors / type issues with intelephense. See Issue #568.

Markdown Source Last Updated:
Mon Mar 15 2021 19:57:32 GMT+0000 (Coordinated Universal Time)
Markdown Source Created:
Mon Aug 19 2019 17:06:24 GMT+0000 (Coordinated Universal Time)
© 2021 Joshua Tzucker, Built with Gatsby