A
A
Alexey Arakelyan2013-02-27 22:26:55
PHP
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){
echo("ok");
}
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)
L
LightSUN, 2013-02-27
@gtbear

    $x = true;
    if ($x == 1 && $x==2 && $x==3)
    {
        echo('ok');
    }

R
robofox, 2013-02-28
@robofox

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

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

M
Melkij, 2013-02-27
@melkij

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

E
egorinsk, 2013-02-27
@egorinsk

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?

A
Anastasia_K, 2013-02-27
@Anastasia_K

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