Return true php
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
A friend got this question during an interview:
What should be the value of x so that the following function returns true.
<?php
function returnTrue( $x )
$x[$x] = $x;
return $x != true;
$res = returnTrue(YOUR_ANSWER);
var_dump($res);
?>
The answer should be 3 characters max
php
add a comment |Â
up vote
6
down vote
favorite
A friend got this question during an interview:
What should be the value of x so that the following function returns true.
<?php
function returnTrue( $x )
$x[$x] = $x;
return $x != true;
$res = returnTrue(YOUR_ANSWER);
var_dump($res);
?>
The answer should be 3 characters max
php
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
A friend got this question during an interview:
What should be the value of x so that the following function returns true.
<?php
function returnTrue( $x )
$x[$x] = $x;
return $x != true;
$res = returnTrue(YOUR_ANSWER);
var_dump($res);
?>
The answer should be 3 characters max
php
A friend got this question during an interview:
What should be the value of x so that the following function returns true.
<?php
function returnTrue( $x )
$x[$x] = $x;
return $x != true;
$res = returnTrue(YOUR_ANSWER);
var_dump($res);
?>
The answer should be 3 characters max
php
php
asked 3 hours ago
TSR
1,04711029
1,04711029
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
That was interesting, I gave this test a quick try and the answer is :
$res = returnTrue();
did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
â Elementary
2 hours ago
add a comment |Â
up vote
1
down vote
of course this
returntrue();
works
but not completely fine as you would receive the message
Warning: Illegal offset type in ....
You must keep in my in mind that string type also allows arrayAccess style so
the right answer is
$res=returntrue('0');
var_dump($res)// print true
When you give as argument the string '0'
this code $x[$x]=$x
give again the same string '0'
wich will not produce any warning and will absolutely return true as the string '0'
will always be evaluated as false
and false!=true
return true
A better (and probably the correct) solution in my opinion.
â Nick
15 mins ago
add a comment |Â
up vote
0
down vote
And also for the other post now deleted, I also managed to solve it.
function returnTrue( $x )
if( !is_array( $x ) )
return false;
foreach( $x as $x )
$x = $x;
return $x;
var_dump(returnTrue([!0]));
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
That was interesting, I gave this test a quick try and the answer is :
$res = returnTrue();
did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
â Elementary
2 hours ago
add a comment |Â
up vote
2
down vote
accepted
That was interesting, I gave this test a quick try and the answer is :
$res = returnTrue();
did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
â Elementary
2 hours ago
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
That was interesting, I gave this test a quick try and the answer is :
$res = returnTrue();
That was interesting, I gave this test a quick try and the answer is :
$res = returnTrue();
answered 2 hours ago
Rémi Bosgaerd
9710
9710
did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
â Elementary
2 hours ago
add a comment |Â
did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
â Elementary
2 hours ago
did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
â Elementary
2 hours ago
did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
â Elementary
2 hours ago
add a comment |Â
up vote
1
down vote
of course this
returntrue();
works
but not completely fine as you would receive the message
Warning: Illegal offset type in ....
You must keep in my in mind that string type also allows arrayAccess style so
the right answer is
$res=returntrue('0');
var_dump($res)// print true
When you give as argument the string '0'
this code $x[$x]=$x
give again the same string '0'
wich will not produce any warning and will absolutely return true as the string '0'
will always be evaluated as false
and false!=true
return true
A better (and probably the correct) solution in my opinion.
â Nick
15 mins ago
add a comment |Â
up vote
1
down vote
of course this
returntrue();
works
but not completely fine as you would receive the message
Warning: Illegal offset type in ....
You must keep in my in mind that string type also allows arrayAccess style so
the right answer is
$res=returntrue('0');
var_dump($res)// print true
When you give as argument the string '0'
this code $x[$x]=$x
give again the same string '0'
wich will not produce any warning and will absolutely return true as the string '0'
will always be evaluated as false
and false!=true
return true
A better (and probably the correct) solution in my opinion.
â Nick
15 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
of course this
returntrue();
works
but not completely fine as you would receive the message
Warning: Illegal offset type in ....
You must keep in my in mind that string type also allows arrayAccess style so
the right answer is
$res=returntrue('0');
var_dump($res)// print true
When you give as argument the string '0'
this code $x[$x]=$x
give again the same string '0'
wich will not produce any warning and will absolutely return true as the string '0'
will always be evaluated as false
and false!=true
return true
of course this
returntrue();
works
but not completely fine as you would receive the message
Warning: Illegal offset type in ....
You must keep in my in mind that string type also allows arrayAccess style so
the right answer is
$res=returntrue('0');
var_dump($res)// print true
When you give as argument the string '0'
this code $x[$x]=$x
give again the same string '0'
wich will not produce any warning and will absolutely return true as the string '0'
will always be evaluated as false
and false!=true
return true
edited 2 hours ago
answered 2 hours ago
Elementary
1,2481215
1,2481215
A better (and probably the correct) solution in my opinion.
â Nick
15 mins ago
add a comment |Â
A better (and probably the correct) solution in my opinion.
â Nick
15 mins ago
A better (and probably the correct) solution in my opinion.
â Nick
15 mins ago
A better (and probably the correct) solution in my opinion.
â Nick
15 mins ago
add a comment |Â
up vote
0
down vote
And also for the other post now deleted, I also managed to solve it.
function returnTrue( $x )
if( !is_array( $x ) )
return false;
foreach( $x as $x )
$x = $x;
return $x;
var_dump(returnTrue([!0]));
add a comment |Â
up vote
0
down vote
And also for the other post now deleted, I also managed to solve it.
function returnTrue( $x )
if( !is_array( $x ) )
return false;
foreach( $x as $x )
$x = $x;
return $x;
var_dump(returnTrue([!0]));
add a comment |Â
up vote
0
down vote
up vote
0
down vote
And also for the other post now deleted, I also managed to solve it.
function returnTrue( $x )
if( !is_array( $x ) )
return false;
foreach( $x as $x )
$x = $x;
return $x;
var_dump(returnTrue([!0]));
And also for the other post now deleted, I also managed to solve it.
function returnTrue( $x )
if( !is_array( $x ) )
return false;
foreach( $x as $x )
$x = $x;
return $x;
var_dump(returnTrue([!0]));
answered 2 hours ago
Rémi Bosgaerd
9710
9710
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52904507%2freturn-true-php%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password