Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Articles Tagged ‘php’

Send PHP errors per mail

Monday, June 25th, 2018 by Servage
Error-handling in PHP can be complex, and getting a good way of having clear and good error reporting and maintaining security, by not displaying to much to the public, can be difficult. Maybe sending errors to developers per email could be a good tool for you. Check out the following example code: // Our custom error handler function nettuts_error_handler($number, $message, $file, $line, $vars) { $email = " <p>An error ($number) occurred on line <strong>$line</strong> and in the <strong>file: $file.</strong> <p> $message </p>"; $email .= "<pre>" . print_r($vars, 1) . "</pre>"; $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Email the error to someone... error_log($email, 1, 'you@youremail.com', $headers); // Make sure that you decide how to respond to errors (on the user's side) // Either ...

HTML parser in PHP

Tuesday, April 24th, 2018 by Servage
Are you ever struggling with HTML content in PHP? Do you need to parse a HTML source? Going through a HTML document in order to extract data might seem like a complicated, or even wrong, way of approaching a problem. Surely there must be another way... Well, sometimes other ways aren't available, sometimes the quick and dirty HTML extraction is simply the easiest way to get things done. Therefore you might find yourself in situations where a working HTML parser for PHP would be a real help. PHP Simple DOM Parser by C.C. Chen is a PHP class that does exactly what I described above. It's available from Sourceforge. It can ...

Create nice documentation for your PHP code

Tuesday, April 3rd, 2018 by Servage
It's a common problem amongh developers that insufficiently or no documentation of code causes problems, delays, misunderstandings. Even code you wrote yourself could look really strange after a while, when you no longer remember what's exactly going on. At this point a good documentation could help you save time and nerves. PhpDocumentor is a good script that generates documentation for your code based on inline comments in your script files. It can generate various output formats, has different themes, and is very customizable. It only requires that you actually commend your code. You can read how to comment your code easily in the PhpDocumentor manual. There are a ...

PHP Handler Engines Explained

Thursday, December 21st, 2017 by Servage
phpAlmost all PHP developers must have heard of either mod_php, PHP-FPM or FastCGI before. However, you might not be aware of what they actually are and do behind the scenes of a web server or how they are different from each other. These handlers are a base requirement for the PHP language to run on web servers so it’s good to know how they operate. PHP Handlers Explained Briefly Regardless of what PHP handler a web server uses, the process of executing PHP code is mostly the same. A web server receives a request to serve a PHP page. It then sends the request to a PHP handler which processes the request, and ...

Documenting PHP APIs

Friday, December 8th, 2017 by Servage
APIA good documentation is a requirement for a good API. If you make a good public API but the documentation is lacking, your API will not see as big of an adoption as it would with a well-written developer-friendly API. Let’s have a look at how to write documentation for a PHP-based API and also what a documentation should have, no matter what language it is written in. Writing a Good API Documentation Before looking at how to write API documentation, let’s consider the things a documentation should have. We will be focusing on a REST API, although the points mentioned usually apply to other types of APIs as well. First of all, every ...

Permission Management in PHP with Bouncer

Saturday, October 21st, 2017 by Servage
php7Permission management doesn’t sound like an easy task. Fortunately, a PHP package called Bouncer makes it rather simple. Bouncer not only handles permissions but also user groups, and you can assign permissions to those groups. For example. You can allow editors to create blog posts with only a few lines of code. Installing Bouncer Since Bouncer is a PHP package, to no surprise it can be installed using Composer by running “composer require silber/bouncer”. Bouncer is a Laravel package so therefore it only works with Laravel projects. As usually with Laravel packages, you have to add the new package to the providers array of config/app.php: “Silber\Bouncer\BouncerServiceProvider::class”. Also add the following to the aliases ...

What has changed in Laravel 5.5?

Monday, September 18th, 2017 by Servage
laravelA new major version of Laravel is scheduled for release in late August 2017. In addition to being a long-term support release, many new features will be added to the framework, including a new error handler, custom validation rules, a new Artisan command and more. Let’s have a closer look at these new features. The “migrate:fresh” Command In previous versions of Laravel, the migrate:refresh command has been used as a quick way to roll back all changes made to a database. What it does is simply execute the down() method of all migrations followed by the up() method. The new command migrate:fresh does the same but skips calling the down() method first. In many ...

PHP best practices for using Traits and Interfaces

Thursday, August 10th, 2017 by Servage
php-codeDo you often notice yourself ending up writing the same code in multiple files? Do you wish you could force similar classes to follow the same structure? Traits and interfaces are something new PHP developers tend to overlook, but they are perfect in solving problems like these. Traits: An Alternative to Multiple Inheritance Traits have been available in PHP since 5.4. A trait is a PHP file that uses the trait keyword instead of class. Traits and classes are quite similar: classes can be extended and traits can be used inside other classes to prevent having to write same code twice in different files. Instead of writing the same method for two classes, ...

Things that every PHP developer should know

Saturday, August 5th, 2017 by Servage
phpThe PHP ecosystem is relatively simple to keep up-to-date on once you know your way around it. The ecosystem has not changed as significantly as let’s say JavaScript over the past few years. However, there are some concepts every developer should be familiar with, as well as some tools that have become standards in PHP. Let’s have a look at these things you should be familiar with as a PHP developer. Composer Composer is the first thing that comes to the mind of developers when they have to name one great thing about PHP. Composer is a package and dependency manager tool for PHP, and it makes working with external libraries much easier ...

What is new in PHP 7.1?

Monday, June 12th, 2017 by Servage
php-7-1PHP 7.1 was released at the end of 2016 and web servers are being upgraded to support the latest additions to the language. This is a good moment for developers to see what new features have been added to PHP in the newest version. Void Return Type Let’s start with something small and simple. PHP already has support for function return types, excluding the void data type. A void return type means that no value is returned from a function. In version 7.1, you can indicate this in the function signature by using the typical colon syntax that already works with other return types: protected function foo(string $value): void {}. Nullable Types PHP also already ...