Categories
PHP

What is a palindrome ?

What is a palindrome ? Find the definition below from the online oxford

a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.

In the example below only a word palindrome could be identified. Additional conditions would be necessary to analyze phrase or sequences

function isPalindrome($str): bool
{
    $str_length = strlen($str);
    $mid = round($str_length / 2);
    $last_character = $str_length - 1;

    for ($i = 0; $i <= $mid; $i++) {
        if ($str[$i] != $str[$last_character]) {
            return false;
        }
        $last_character--;
    }
    return true;
}

var_dump( isPalindrome('madam'));
var_dump( isPalindrome('nursesrun'));
var_dump( isPalindrome('davad'));
var_dump( isPalindrome('avaava'));

What is the run time complexity ?

It sounds to be O(n/2)

What is the space complexity ?

We do not allocate memory except for few variables as $i, $mid and $last_character

O(1)

Categories
BEST PHP CODE PHP php8

Null Safe operator is awesome ! Great code

Awesome piece of work, imagine to replace the follwoing piece of code

$country =  null;
 
if ($session !== null) {
    $user = $session->user;
 
    if ($user !== null) {
        $address = $user->getAddress();
 
        if ($address !== null) {
            $country = $address->country;
        }
    }
}
 

by just

$country = $session?->user?->getAddress()?->country; 

Categories
BEST PHP CODE PHP PHPUnit

Useful catch (\Throwable $e)

catch (\Throwable $e) turned out to be very useful while coding in PHP.

When Exception were caught in specific instances, catch throwable would catch anything that would impede the code to run

Example below in a phpUnit scenario :

        try {
            $r = $this->checkKeys($data);
        } catch (\Throwable $e) {
            $this->assertEquals($e->getCode(), 404);
        }
Categories
PHP

Developer Manifesto by David Raleche

4 Principles are guiding this methodology


- Readability
- Quality Assurance
- Logic
- Reusable code

 Readability


PSR-2 code styling
Function no longer than 50 lines
PHP doc Block
TypeHint your functions

More information:

For better code readability we suggest PSR-2 code styling to be applied. We consider a function bigger than 25 lines is not an efficient function. For engaging this methodology we recommend 50 lines maximum per function. DocBloc are essentials. Swagger in a case of API writing is essential to be present.
A developer should be able to explain his work easily and quickly and should use materials to do so

Quality Insurance


No PHP error message
No PHP Warning messages
No PHP Notices
No hardcore credentials
Unit Testing
Details:

When executing the code we expect the quality assurance team to verify that no PHP errors, warnings, notices are produced. We also expect the QA team to go throughout the code to verify that no hardcore credentials are present within the code. Unit Test writing should be edited by QA team preferably

Logic


DRY (do not repeat yourself)
KISS (Keep it Stupid Simple)
SOLID principles

 Reusable Code


Use PHP throw exception
Use Object oriented techniques
Use static function

Use PHP throw exception
Use Object oriented techniques
Use static function

Add the following your PHP too get rid of PHP notices, warnings

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


Want to know more about my work around the LAMP stack world and other specialties? Sign up for our email brief for hand-picked articles, news, and more.

Subscribe

* indicates required

Categories
BEST PHP CODE PHP

How to address the undefined function yaml_parse_file ?

Here are the steps when you encounter the error below, it means that the yaml library is missing as it is not native to php per say

undefined function yaml_parse_file

Install YAML library for PHP when you see the error below

yum search yaml

First step is to install yaml via pecl

pecl install yaml-2.0.0

Second step is to create the following file

/etc/php/conf.d/ext-yaml.ini

And add the following line

echo "extension=yaml.so"

Then restart php server

service php-fpm restart
Categories
BEST PHP CODE PHP

How to setup a CRON JOB for apache/httpd to restart automatically ?


Very helpful website top figure out crontab sequence

https://crontab.guru/#0_0_*_*_*

Every 30 minutes 

/etc/crontab

  */30  *  *  *  *       root /etc/init.d/httpd restart > /dev/null 2>&1

  */30  *  *  *  *       root /etc/init.d/mysql restart > /dev/null 2>&1

Every hour

/etc/crontab

  0  *  *  *  *       root /etc/init.d/httpd restart > /dev/null 2>&1

Every minute

/etc/crontab

  *  *  *  *  *       root /etc/init.d/httpd restart > /dev/null 2>&1

Check logs 

tail n- 200  /var/log/cron

tail n- 200  /var/log/messages

Plus link here regarding dev/null 2>&1

————————————————————————

Categories
BEST PHP CODE PHP

How to use PHP DocBlock ?

/**
 * Returns a list of XXXXXXX
 *
 * @param string $route_group_uuid
 *
 * @return Array $listCarrierRoute
 *
 * @throws \Exception
 *
 * @version 05-05-2020
 * @author David Raleche <[email protected]>
 * @reference JIRA Ticket PMM2-862 Create route groups XXXXX
 */
Categories
BEST PHP CODE EasyPhp PHP

What to do – PHP session slow ?

Solution

Increase memory size

memory_limit = 2048M
Categories
PHP

PHP Xcache to be light fast

yum install php xcache – light fast application

 yum install php54-xcache.x86_64
Categories
PHP

How to clear cache in Symfony ?

rm -rf var/cache/*