PHP Spaceship Operator <=>

Code sur écran d'ordinateur

A little-known but very useful operator, for example to sort an array, is the spaceship operator: <=> It compares two values and returns an integer (-1, 0, 1) depending on the result. $a < $b returns -1 $a = $b returns 0 $a > $b returns 1 One way to see it is here: https://3v4l.org/WcRSo … Read more

Categories PHP

PHP CS Fixer – How to keep your code clean

Code sur écran d'ordinateur

A really useful tool to keep your code clean is PHP Coding Standards Fixer. It allows us to follow the PHP standards defined in PSR-1, PSR-2, etc. It is also possible to configure it to follow the style of your team with a documentation. The recommended way to install it is outside your code, but … Read more

Xdebug installation with PHP 5.6 on Ubuntu

MacBook avec code sur un bureau

First of all you have to install the xdebug package for PHP 5.6 sudo apt-get install php-xdebug Then you have to enable it so it is accessible from PhpStorm for example Edit the file sudo nano /etc/php/5.6/mods-available/xdebug.ini and add the following lines xdebug.remote_enable = on xdebug.remote_connect_back = on xdebug.idekey = “vagrant” The PHP service needs … Read more

Categories PHP

PHP: Create a date/ timestamp with strtotime by setting the time

Code sur écran d'ordinateur

Everyone knows the strtotime function. With it, for example, we can retrieve the date of 2 weeks ago, strtotime(‘-2 week’); or a year ago strtotime(‘-1 year’); What is interesting is that you can retrieve the date of 2 weeks ago while defining the time, for example midnight strtotime(‘-2 week midnight’); or noon: strtotime(‘-2 week noon’); … Read more

Categories PHP

PHP – DOMDocument and XPath to parse a large XML file

Code sur écran d'ordinateur

When we have to process a very large XML, it is unfortunately not possible to use SimpleXML. Fortunately for this, we can use DOMDocument. To do this, just load the desired file and initialize xpath on it with DOMXPath. $document = new DOMDocument(); $document->load($file); $xpath = new DOMXPath($document); If the XML in question has a … Read more

Categories PHP

PHP and SimpleXML

Code sur écran d'ordinateur

When you want to process an XML file in PHP, there are two possibilities: either we use SimpleXML or we go through DOMDocument. In this article I will deal more specifically with SimpleXML. To load a file, just use the simplexml_load_file method $xml = simplexml_load_file($file); By doing so, you get a SimpleXMLElement. If the XML … Read more

Categories PHP

PHP – How to load a FILE in PHP from a form

Code sur écran d'ordinateur

To find out how to load a file in PHP from a form, you have to start by formatting the form correctly. It is mandatory to use the post method as well as the multipart/form-data enctype, otherwise PHP will not receive the necessary data. <form action=”/” method=”post” enctype=”multipart/form-data”> <input type=”file” name=”namefile” /> <input type=”submit” value=”ok” … Read more

Categories PHP

PHP – How to find the origin of the call on a PHP function

Code sur écran d'ordinateur

It can happen that a function is only callable from one interface (console, apache…). There is a function that returns exactly what we are looking for. In the following example, we are looking to know whether the call is from the console. if (php_sapi_name() != “cli”) { header(“Location: http://www.sadtrombone.com/”); die(); }

Categories PHP

PHP – Protection of special characters of regular expressions

Code sur écran d'ordinateur

If you want to use a regular expression to, for example, search for a word in a paragraph, it is important to protect your variable. To do this, just use the following function: string preg_quote ( string $str [, string $delimiter = NULL ] ) For example: foreach ($aRequest[‘keywords’] as $sKeyword) { $sKeyword = preg_quote($sKeyword); … Read more

Categories PHP