PHP, SQL and Laravel Job Interview Questions

Lecture



1. What will this output? Why is the type cast to integer and not the other way around?

<?php

$a = "test";

if ($a == 0) {

echo "1";

} else {

echo "2";

}

var_dump('123' == ' 123'); // true

var_dump('1e3' == '1000'); // true

var_dump('+74951112233' == '74951112233'); // true

var_dump('00000020' == '0000000000000000020'); // true

var_dump('0X1D' == '29E0'); // true

var_dump('0xafebac' == '11529132'); // true

var_dump('0xafebac' == '0XAFEBAC'); // true

var_dump('0xeb' == '+235e-0'); // true

var_dump('0.235' == '+.235'); // true

var_dump('0.2e-10' == '2.0E-11'); // true

var_dump('61529519452809720693702583126814' == '61529519452809720000000000000000'); // true in php < 5.4.

If you compare a number with a string, or two strings that contain numbers, each string is converted to a number and they are compared as numbers. These rules also apply to the switch statement. No type conversion happens with === or !==, because in that case the types are compared in addition to the values.

Converting strings to numbers

If a string is recognized as a numeric value, the resulting value and type are determined as described below.

If the string does not contain any of the characters '.', 'e', or 'E', and the numeric value fits within the integer range (defined by PHP_INT_MAX), the string is treated as an integer. In all other cases it is treated as a floating-point number (float).

The value is determined by the initial part of the string. If the string starts with a valid numeric value, that value is used. Otherwise the value is 0 (zero). A valid numeric value is one or more digits (which may contain a decimal point), optionally preceded by a sign, followed by an optional exponent. An exponent is an 'e' or 'E' followed by one or more digits.

Comparison of $x types and results of PHP type-related functions
Expression gettype() empty() is_null() isset() boolean : if($x)
$x = ""; string TRUE FALSE TRUE FALSE
$x = null; NULL TRUE TRUE FALSE FALSE
var $x; NULL TRUE TRUE FALSE FALSE
$x is not defined NULL TRUE TRUE FALSE FALSE
$x = array(); array TRUE FALSE TRUE FALSE
$x = array('a', 'b'); array FALSE FALSE TRUE TRUE
$x = false; boolean TRUE FALSE TRUE FALSE
$x = true; boolean FALSE FALSE TRUE TRUE
$x = 1; integer FALSE FALSE TRUE TRUE
$x = 42; integer FALSE FALSE TRUE TRUE
$x = 0; integer TRUE FALSE TRUE FALSE
$x = -1; integer FALSE FALSE TRUE TRUE
$x = "1"; string FALSE FALSE TRUE TRUE
$x = "0"; string TRUE FALSE TRUE FALSE
$x = "-1"; string FALSE FALSE TRUE TRUE
$x = "php"; string FALSE FALSE TRUE TRUE
$x = "true"; string FALSE FALSE TRUE TRUE
$x = "false"; string FALSE FALSE TRUE TRUE

Loose comparison with ==
TRUE FALSE 1 0 -1 "1" "0" "-1" NULL array() "php" ""
TRUE TRUE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE
FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE
1 TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
0 FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE
-1 TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
"1" TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
"0" FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
"-1" TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
NULL FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE
array() FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
"php" TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
"" FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE

Strict comparison with ===
TRUE FALSE 1 0 -1 "1" "0" "-1" NULL array() "php" ""
TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
1 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
0 FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
-1 FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
"1" FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
"0" FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
"-1" FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
NULL FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
array() FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
"php" FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
"" FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE

2.1 What will this output?

<?php 

class Foo {

?>

<?php

function bar() {

print "bar";

}

}

$foo = new Foo();

$foo->bar();

it will output an error

Parse error: syntax error, unexpected '?>', expecting function (T_FUNCTION) or const (T_CONST) in /home//prog.php on line 4

2.2 Why is there no error here?

class Foo {

function bar() {

?> <?php

print "bar";

}

}

$foo = new Foo();

$foo->bar();

3. What will this output?

<?php 

class MyException extends Exception {}

class MegaException extends MyException {}

try {

throw new MyException();

} catch (MyException $e) {

//Shit happens

throw new MegaException();

} catch (MegaException $e) {

echo 'aaa';

} catch (Exception $e) {

echo 'bbb';

}

echo 'ccc';

answer - it will output an error, because throw new MegaException(); is outside the try/catch

Fatal error: Uncaught MegaException in /home/prog.php:10

Stack trace:

#0 {main}

thrown in /home//prog.php on line 10

you need to remove throw new MegaException

<?php 

class MyException extends Exception {};

class MegaException extends MyException {};

try {

throw new MegaException();

} catch (MegaException $e) {

echo 'aaa';

} catch (MyException $e) {

//Shit happens

throw new MegaException();

} catch (Exception $e) {

echo 'bbb';

}

echo 'ccc';

4. How to change the code so it outputs

aaa

bbb

ccc

5. What will this output?

<?php 

function a(&$bar)

{

$bar *= 2;

return $bar;

}

$x =3;

$y = a($x);

$x = 5;

echo $x.' '.$y;

answer "6 5 "

6. What needs to be changed so that it outputs "5 5"

(changing (extending) only the references)

solution option 6.1

<?php 

function &a(&$bar)

{

$bar *= 2;

return $bar;

}

$x =3;

$y = &a($x);

$x = 5;

echo $x.' '.$y;

$x = 5;

echo $x.' '.$y;

solution option 6.2

<?php 

function a(&$bar)

{

$bar *= 2;

return $bar;

}

$x =3;

$y = a($x);

$y = &$x;

$x = 5;

echo $x.' '.$y;

6.7 What will this code output

$a = '1';

$b = &$a;

$b = "2$b";

echo $a.", ".$b; and

6.8 What will this code output

$i = 016;

echo $i / 2;

7. Write code that finds the non-repeating element of an array (5)

<?php 

$a = [3, 4, 7, 1, 2, 5, 7, 4, 3, 1, 2];

$b = [];

foreach($a as $k1=>$v1)

{

foreach($a as $k=> $v)

{

if( $v1=== $v && $k1 != $k )

$b[$v]=1;

}

}

foreach($a as $k=> $vb)

{

if (!isset($b[$vb])) echo $vb;

}

8. There are 2 tables

table authors

id

table articles

id

author_id

8.1 Output each author along with the number of articles they have

8.2 Output the authors who have more than 3 articles

solution

SELECT au.id, COUNT(ar.id) cnt

FROM authors au

LEFT JOIN articles ar ON ar.author_id= au.id

GROUP BY au.id

HAVING cnt>=4

9. What is the difference between an interface and an abstract class?

Answer

An abstract class is a class that has at least one abstract (undefined) method; it is marked with the abstract keyword.

An interface is the same kind of abstract class, except that it cannot have properties and its methods have no bodies defined.

It is also worth noting that an abstract class is inherited (extends), while an interface is implemented (implements). This is where the difference comes from: we can inherit only one class, but implement as many interfaces as we like.
IMPORTANT! When implementing an interface, you must implement all of its methods, otherwise a Fatal error occurs; this can also be avoided by declaring the class with the abstract keyword.
Example:


interface I {
    public function F();
    public function say();
}
abstract class A implements I {
    function say() {
        echo 'Hello';
    }
    // function F() - not implemented
}

10. What visibility levels exist for methods in PHP? Tell me about them.

Answer

Inheritance is one of the three core concepts of OOP (the other two are encapsulation and polymorphism). Access modifiers were implemented precisely for it. The basic idea of inheritance: a child object, when inheriting (extend) from a parent, takes over all of the parent's methods and properties, and can also acquire its own. With this foundation understood, we can move on to everything below...

Private — declares a method or property accessible only within the class in which it is defined. That is, we cannot access private methods and properties either from objects or from child classes.

Protected — declares a method or property as protected. That is, it cannot be accessed from an object instantiating the class, but it can be used in child classes.

Public — public. Classes and methods declared public can be accessed both inside the class itself and in child classes and in objects instantiating the class.

I should note right away that during inheritance, access levels can only be changed to more permissive ones, i.e. in the following order, but not the other way around: private → protected → public

Methods can also be final that is, they cannot be overridden in descendant classes.

11. Laravel php artisan: what does it do, and which commands do you run through it most often?

Artisan is the name of the command-line interface included with Laravel

php artisan list

RUNNING COMMANDS for the application

12. Which middleware have you used, and what is it?

HTTP middleware (intermediaries) are filters for processing an HTTP request. For example, Laravel includes middleware for checking user authentication.

Laravel has several default middleware classes located in the app/Http/Middleware folder. These implement site maintenance mode ("the site is temporarily down, please come back later"), authorization checks, CSRF protection, and so on.

13. What is the SERVICE CONTAINER?

The Service Container (formerly the IoC container) is a powerful tool for managing class dependencies.

The Service Container (formerly the IoC container) is a powerful tool for managing class dependencies. In modern web development there is a fashionable term, Dependency Injection, which means injecting certain classes into the class being created via the constructor or a setter method. The class being created uses these classes in its work. The service container implements exactly this functionality.

Simplifying a bit, we can say this: when the framework needs to create a class, it does not use the construct new SomeClass(new SomeService()), but App::make('SomeClass'), having previously registered a function that creates the SomeClass class and all the classes that SomeClass accepts as constructor arguments

13.1 Registering a regular class

$this->app->bind('FooBar', function($app)

{ return new FooBar($app['SomethingElse']);

});

Registering a singleton class

$this->app->singleton('FooBar', function($app)

{ return new FooBar($app['SomethingElse']);

});

13.2 Resolving from the container

There are several ways to resolve the contents of the container.

13.2.1 First, you can use the make() method:

$fooBar = $this->app->make('FooBar');

13.2.2 Second, you can access the container as an array:

$fooBar = $this->app['FooBar'];

13.2.3 And finally, third (and most importantly), you can explicitly type-hint an argument in the class constructor and the framework will take it from the container itself (in the example below it is UserRepository):

public function __construct(UserRepository $users)

{

$this->users = $users;

}

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Running server side scripts using PHP as an example (LAMP)"

Terms: Running server side scripts using PHP as an example (LAMP)