diff --git a/_posts/01-03-01-Built-in-Web-Server.md b/_posts/01-03-01-Built-in-Web-Server.md index 79b0ddb..8513a85 100644 --- a/_posts/01-03-01-Built-in-Web-Server.md +++ b/_posts/01-03-01-Built-in-Web-Server.md @@ -8,7 +8,9 @@ anchor: builtin_web_server With PHP 5.4 or newer, you can start learning PHP without installing and configuring a full-fledged web server. To start the server, run the following command from your terminal in your project's web root: - > php -S localhost:8000 +{% highlight console %} +> php -S localhost:8000 +{% endhighlight %} * [Learn about the built-in, command line web server][cli-server] diff --git a/_posts/03-05-01-Command-Line-Interface.md b/_posts/03-05-01-Command-Line-Interface.md index c79b351..f0fd756 100644 --- a/_posts/03-05-01-Command-Line-Interface.md +++ b/_posts/03-05-01-Command-Line-Interface.md @@ -11,7 +11,7 @@ CLI PHP programs are powerful because you can use your app's code directly witho Try running PHP from your command line: -{% highlight bash %} +{% highlight console %} > php -i {% endhighlight %} @@ -37,7 +37,7 @@ The `exit()` expression is used with a non-zero number to let the shell know tha To run our script, above, from the command line: -{% highlight bash %} +{% highlight console %} > php hello.php Usage: php hello.php [name] > php hello.php world diff --git a/_posts/03-06-01-XDebug.md b/_posts/03-06-01-XDebug.md index 0f0622b..598b5b4 100644 --- a/_posts/03-06-01-XDebug.md +++ b/_posts/03-06-01-XDebug.md @@ -20,8 +20,10 @@ want to enable right away. Traditionally, you will modify your Apache VHost or .htaccess file with these values: - php_value xdebug.remote_host=192.168.?.? - php_value xdebug.remote_port=9000 +{% highlight ini %} +php_value xdebug.remote_host=192.168.?.? +php_value xdebug.remote_port=9000 +{% endhighlight %} The "remote host" and "remote port" will correspond to your local computer and the port that you configure your IDE to listen on. Then it's just a matter of putting your IDE into "listen for connections" mode, and loading the URL: diff --git a/_posts/04-02-01-Composer-and-Packagist.md b/_posts/04-02-01-Composer-and-Packagist.md index 23d33b4..497ffb1 100644 --- a/_posts/04-02-01-Composer-and-Packagist.md +++ b/_posts/04-02-01-Composer-and-Packagist.md @@ -13,7 +13,9 @@ There are already a lot of PHP libraries that are compatible with Composer, read You can install Composer locally (in your current working directory; though this is no longer recommended) or globally (e.g. /usr/local/bin). Let's assume you want to install Composer locally. From your project's root directory: - curl -s https://getcomposer.org/installer | php +{% highlight console %} +curl -s https://getcomposer.org/installer | php +{% endhighlight %} This will download `composer.phar` (a PHP binary archive). You can run this with `php` to manage your project dependencies. Please Note: If you pipe downloaded code directly into an interpreter, please read the code online first to confirm it is safe. @@ -32,26 +34,34 @@ Manually installing Composer is an advanced technique; however, there are variou Since a manual installation performs none of these checks, you have to decide whether the trade-off is worth it for you. As such, below is how to obtain Composer manually: - curl -s https://getcomposer.org/composer.phar -o $HOME/local/bin/composer - chmod +x $HOME/local/bin/composer +{% highlight console %} +curl -s https://getcomposer.org/composer.phar -o $HOME/local/bin/composer +chmod +x $HOME/local/bin/composer +{% endhighlight %} The path `$HOME/local/bin` (or a directory of your choice) should be in your `$PATH` environment variable. This will result in a `composer` command being available. When you come across documentation that states to run Composer as `php composer.phar install`, you can substitute that with: - composer install - +{% highlight console %} +composer install +{% endhighlight %} + This section will assume you have installed composer globally. ### How to Define and Install Dependencies Composer keeps track of your project's dependencies in a file called `composer.json`. You can manage it by hand if you like, or use Composer itself. The `composer require` command adds a project dependency and if you don't have a `composer.json` file, one will be created. Here's an example that adds [Twig][2] as a dependency of your project. - composer require twig/twig:~1.8 +{% highlight console %} +composer require twig/twig:~1.8 +{% endhighlight %} Alternatively the `composer init` command will guide you through creating a full `composer.json` file for your project. Either way, once you've created your `composer.json` file you can tell Composer to download and install your dependencies into the `vendor/` directory. This also applies to projects you've downloaded that already provide a `composer.json` file: - composer install +{% highlight console %} +composer install +{% endhighlight %} Next, add this line to your application's primary PHP file; this will tell PHP to use Composer's autoloader for your project dependencies. diff --git a/_posts/04-03-01-PEAR.md b/_posts/04-03-01-PEAR.md index c00b819..abebbff 100644 --- a/_posts/04-03-01-PEAR.md +++ b/_posts/04-03-01-PEAR.md @@ -27,8 +27,10 @@ have an apt ``php-pear`` package. If the package is listed on the [PEAR packages list][3], you can install it by specifying the official name: +{% highlight console %} pear install foo - +{% endhighlight %} + If the package is hosted on another channel, you need to `discover` the channel first and also specify it when installing. See the [Using channel docs][4] for more information on this topic. @@ -71,6 +73,7 @@ available through the Composer autoloader: To use this PEAR package simply reference it like so: {% highlight php %} + echo $foo; Notice: Undefined variable: foo in php shell code on line 1 @@ -21,7 +21,7 @@ Notice: Undefined variable: foo in php shell code on line 1 This is only a notice error, and PHP will happily carry on. This can be confusing for those coming from "exception-heavy" languages, because referencing a missing variable in Python for example will throw an exception: -{% highlight python %} +{% highlight console %} $ python >>> print foo Traceback (most recent call last): @@ -50,6 +50,7 @@ predefined error level constants, meaning if you only want to see Warnings and E you can configure that: {% highlight php %} +development, configure the following settings in your `php.ini`: - display_errors = On - display_startup_errors = On - error_reporting = -1 - log_errors = On +{% highlight ini %} +display_errors = On +display_startup_errors = On +error_reporting = -1 +log_errors = On +{% endhighlight %} > Passing in the value `-1` will show every possible error, even when new levels and constants are added in future PHP versions. The `E_ALL` constant also behaves this way as of PHP 5.4. - [php.net](http://php.net/function.error-reporting) @@ -36,10 +38,12 @@ use either `-1` or `E_ALL | E_STRICT`. To hide errors on your production environment, configure your `php.ini` as: - display_errors = Off - display_startup_errors = Off - error_reporting = E_ALL - log_errors = On +{% highlight ini %} +display_errors = Off +display_startup_errors = Off +error_reporting = E_ALL +log_errors = On +{% endhighlight %} With these settings in production, errors will still be logged to the error logs for the web server, but will not be shown to the user. For more information on these settings, see the PHP manual: diff --git a/_posts/13-03-01-Docker.md b/_posts/13-03-01-Docker.md index 36fa829..bf0e669 100644 --- a/_posts/13-03-01-Docker.md +++ b/_posts/13-03-01-Docker.md @@ -17,7 +17,7 @@ After you [installed docker][docker-install] on your machine, you can start an A The following command will download a fully functional Apache installation with the latest PHP version and provide the directory `/path/to/your/php/files` at `http://localhost:8080`: -{% highlight bash %} +{% highlight console %} docker run -d --name my-php-webserver -p 8080:80 -v /path/to/your/php/files:/var/www/html/ php:apache {% endhighlight %} diff --git a/pages/Design-Patterns.md b/pages/Design-Patterns.md index 782837a..47f8301 100644 --- a/pages/Design-Patterns.md +++ b/pages/Design-Patterns.md @@ -203,7 +203,6 @@ The next snippet of code outlines how a calling client class might use one of th behaviour required at runtime: {% highlight php %}