Alexey Arakelyan2013-02-27 22:26:55
Alexey Arakelyan, 2013-02-27 22:26:55

A curious question from a test task?

Hello habravians. They sent me test tasks from one office, and among the questions there is this:
Given the code
$x = ....;
if ($x == 1 && $x==2 && $x==3){
What needs to be substituted in $x so that “ok” is displayed in the console.
I went through all the options in my head, but I never figured out how one variable can go through three comparisons at once with different values. Perhaps this question is about knowing some PHP features, but from my point of view, this task does not have a solution.
So the question is: does this problem have a solution?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
LightSUN, 2013-02-27

    $x = true;
    if ($x == 1 && $x==2 && $x==3)

robofox, 2013-02-28

$x = print("ok");
if ($x == 1 && $x==2 && $x==3){

They did not specify that the instruction with the condition should work;)

Melkij, 2013-02-27

The answer is banal and the problem lies in type casting.
$x = true;

egorinsk, 2013-02-27

I do not understand the purpose for which such tasks are given (yes, I do not remember by heart the rules for casting types). Are they looking for people who will code for them in this style? Are they looking for lovers of memorizing documentation?

Anastasia_K, 2013-02-27

I do not know php at all, but at the level of intuition: it is necessary that in $X there would be an int type. then when comparing type int with any number, true will be returned

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question