Some PHP developer interview algorithms you must now

FizzBuzz:

http://3v4l.org/mIBmo

<?php

for ($i = 0; $i <= 30; $i++) {
    if ($i % 3 == 0 && $i % 5 == 0) {
        echo "$i: FizzBuzz; ";
    } elseif ($i % 3 == 0) {
        echo "$i: Fizz; ";
    } elseif ($i % 5 == 0) {
        echo "$i: Buzz; ";
    }
}

Swap 2 vars values without using a third one:

Numbers and strings with XOR:

http://3v4l.org/dsBvh

<?php

$a = 3;
$b = 5;

$a = $a+$b;
$b = $a-$b;
$a = $a-$b;

echo "a = $a; b = $b";

and:

http://3v4l.org/aqWcl

<?php

for ($i = 0; $i <= 30; $i++) {
    if ($i % 3 == 0 && $i % 5 == 0) {
        echo "$i: FizzBuzz; ";
    } elseif ($i % 3 == 0) {
        echo "$i: Fizz; ";
    } elseif ($i % 5 == 0) {
        echo "$i: Buzz; ";
    }
}

Swap 2 strings without using XOR:

http://3v4l.org/FDFDF

<?php
$a = 'texta';
$b = 'textb';
$a = $a . $b; //textatextb
$b = substr($a, 0, strlen($a) - strlen($b)); //texta
$a = substr($a, strlen($b)); //textb
echo "a = $a; b = $b";

Owncloud: How to change the admin password from the command line.

I’ve been playing with Owncloud in a Docker container today and something was not set right. The admin password was not the default one. So my options were to delete the sqlite db file or run some password generating commands and queries to update the password.

The first thing is to generate a new sha1 password:

echo -n "password" | sha1sum

Use the result from the command above as the new password.

The second thing is to open the sqlite database with the sqlite3 command and update the admin’s password:

cd /var/www/owncloud/data
sqlite3 owncloud.db
update oc_users set password = "newsha1password" where uid = "admin";
.quit

Now you can login normally with admin/newpassword and from there you can set up all of the rest.

The sources I used for this article: here and here.

PSR-0 & PSR-4 Autoloading

As of 2014-10-21 PSR-0 has been marked as deprecated. PSR-4 is now recommended as an alternative.

This citation is from here: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

The PSR-0 Autoloading is now deprecated and we’re going to use only PSR-4 from now on.

Using namespaces and class names without checks if the file exists or looping through all of the files in many directories is way better!