Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,227 members, 7,818,772 topics. Date: Monday, 06 May 2024 at 02:04 AM

Can You Solve PHP Problems? Let's Find Out.. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Can You Solve PHP Problems? Let's Find Out.. (3402 Views)

Can You Solve This Tricky Question? / Number Generator Challenge : Can You Solve This? / Can You Solve This? (2) (3) (4)

(1) (2) (Reply) (Go Down)

Can You Solve PHP Problems? Let's Find Out.. by Nobody: 12:18pm On Oct 08, 2016
Have you been coding PHP for many years but have not gone beyond few functions?, this thread tests your practical knowledge of the language.
Please, don't post off-topic here, just serious contributions involving code and useful comments..

I'd take this from easy to hard and anybody can drop in a question at anytime.

Now, first question:


$first_string = 'DanielTheGeek' ;
$second_string = 'Daniel' ;
if (strpos($first_string, $second_string)) {
echo "\"" . $first_string . "\" contains \"" . $second_string . "\"" ;
} else {
echo "\"" . $first_string . "\" does not contain \"" . $second_string . "\"" ;
}

If you run the above code, you'd get:
"DanielTheGeek" does not contain "Daniel"

Why?

What needs to be changed to make the above code work as expected?

UPDATE: Five points for the first correctly answered question, a leaderboard will be published with the names of people that have high points.
Re: Can You Solve PHP Problems? Let's Find Out.. by phensbassey: 12:24pm On Oct 08, 2016
\"" . $second_string . "\"" ; }
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 12:35pm On Oct 08, 2016
phensbassey:
\"" . $second_string . "\"" ; }
Wrong
Re: Can You Solve PHP Problems? Let's Find Out.. by FrankLampard: 12:37pm On Oct 08, 2016
DanielTheGeek:
Have you been coding PHP for many years but have not gone beyond few functions?, this thread tests your practical knowledge of the language.
Please, don't post off-topic here, just serious contributions involving code and useful comments..

I'd take this from easy to hard and anybody can drop in a question at anytime.

Now, first question:



If you run the above code, you'd get:
"DanielTheGeek" does not contain "Daniel"

Why?

What needs to be changed to make the above code work as expected?

UPDATE: Five points for the first correctly answered question, a leaderboard will be published with the names of people that have high points.

I think that function must be passed with 3 parameters.

The last one being the position to start the search from.

Will look at the code very well later
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 12:39pm On Oct 08, 2016
FrankLampard:


I think that function must be passed with 3 parameters.

The last one being the position to start the search from.

Will look at the code very well later

Put it in form of a solution and let's see, you are wrong tho.

Hint: How is it supposed to work normally?
Re: Can You Solve PHP Problems? Let's Find Out.. by losprince(m): 12:40pm On Oct 08, 2016
the function strpos() is case-sentitive, you should use stripos() instead
Re: Can You Solve PHP Problems? Let's Find Out.. by toheebDOTcom(m): 12:41pm On Oct 08, 2016
@danielthegeek

Its because the return value is 0 which is interpreted as false by the if condition. To correct that, you will check the TYPE as well using ===

if (strpos(...) !== false) {
echo 'contain'
;
} else {
echo 'does not contain'
;
}
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 12:45pm On Oct 08, 2016
losprince:
the function strpos() is case-sentitive, you should use stripos() instead
Case sensitivity is not the problem here..

Again, a plainer hint: What does strpos() do, and what does it return when the condition in our case is not met.
Re: Can You Solve PHP Problems? Let's Find Out.. by FrankLampard: 12:51pm On Oct 08, 2016
toheebDOTcom:
@danielthegeek

Its because the return value is 0 which is interpreted as false by the if condition. To correct that, you will check the TYPE as well using ===

I wanted to say that, but I thought of other possibilities.

You can assign the strpos() to a variable and then check if true or false.

Eg
$strpos = strpos($first_string, $second_string);
if($strpos !== false)
{
True
}
False

NB: I No test sha, and I no look documentation.
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 12:52pm On Oct 08, 2016
toheebDOTcom:
@danielthegeek

Its because the return value is 0 which is interpreted as false by the if condition. To correct that, you will check the TYPE as well using ===

Impressive, very close I must say...but still wrong.
I like the way you think tho.

Remember the result of the strpos() function isn't stored in a variable, so think, and
try again and this time, in form of code.
Re: Can You Solve PHP Problems? Let's Find Out.. by toheebDOTcom(m): 12:52pm On Oct 08, 2016
Where is my 5 points danielthegeek?
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 12:58pm On Oct 08, 2016
toheebDOTcom:
Where is my 5 points danielthegeek?

Write your answer in form of code please, remember, newbies are watching.
Re: Can You Solve PHP Problems? Let's Find Out.. by toheebDOTcom(m): 12:58pm On Oct 08, 2016
I don put code oo danielthegeek.


@franklampard Yea!. That is the only reason
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 1:06pm On Oct 08, 2016
toheebDOTcom:
@danielthegeek

Its because the return value is 0 which is interpreted as false by the if condition. To correct that, you will check the TYPE as well using ===

if (strpos($string_one, $string_two) !== false) {
echo 'contain'
} else {
echo 'does not contain'
}

Okay, ToheebDOTcom provided the correct answer...thumbs up FrankLampard (also correct but late) and the rest for trying.

Beginners, please note the use of !== over !=. If != was used, 0 will be falsy when referenced in a boolean expression.

1 Like

Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 1:10pm On Oct 08, 2016
So ToheebDOTcom has 5 points.
Next question:
What will be the outcome of:
$foo = true and false; var_dump($foo) ;
And why?
Re: Can You Solve PHP Problems? Let's Find Out.. by toheebDOTcom(m): 1:13pm On Oct 08, 2016
DanielTheGeek:
So ToheebDOTcom has 5 points.

Next question:

What will be the outcome of:


And why?

var_dump returns bool.


Modify your question correctly.
Did you mean...
$foo = 'true and false';

or

$foo = true;

$foo = false;

If it is the first case

var_dump returns string



Answer to your modified question..
it returns false..

since it is not in quotes, then 'and'is evaluated as a logical operator. by truth table, truth and false results to false...

Chikena!
Re: Can You Solve PHP Problems? Let's Find Out.. by FrankLampard: 1:17pm On Oct 08, 2016
Signs Out of thread
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 1:18pm On Oct 08, 2016
toheebDOTcom:


var_dump returns bool.


Modify your question correctly.
Did you mean...
$foo = 'true and false';

or

$foo = true;

$foo = false;

If it is the first case

var_dump returns string

I meant what I wrote at first (which is indirectly the second case here), true and false without being encapsulated in quotes.
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 1:19pm On Oct 08, 2016
FrankLampard:
Signs Out of thread
Why?
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 1:21pm On Oct 08, 2016
toheebDOTcom:

var_dump returns bool.

Modify your question correctly. Did you mean... $foo = 'true and false';
or
$foo = true;
$foo = false;
If it is the first case
var_dump returns string
What boolean exactly does it return? True or False? And most importantly, why?
Re: Can You Solve PHP Problems? Let's Find Out.. by toheebDOTcom(m): 1:25pm On Oct 08, 2016
DanielTheGeek:

What boolean exactly does it return? True or False? And most importantly, why?

I modified my answer. I will explain now
Re: Can You Solve PHP Problems? Let's Find Out.. by toheebDOTcom(m): 1:32pm On Oct 08, 2016
I'm enjoying your questions.

But I need to do something now. I will be back to answer those not yet answered. For the sake of knowledge!

Nice one!

1 Like

Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 1:33pm On Oct 08, 2016
toheebDOTcom:


Answer to your modified question..
it returns false

You are right about the outcome being of a boolean data-type but the value it returns is TRUE not FALSE.

Nice try ToheebDOTcom.

Here's why it returns TRUE:
The issue here is that the = operator is more superior to the and operator in order of operations.

Merely glancing at the code, one would expect it to return FALSE, but if you take a second look, it would only have done as we expected if we wrapped it in parenthesis as follows:

$foo = (true and false) ;
I'm happy, Asalimpo is here...great mind that is.
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 1:43pm On Oct 08, 2016
Next question:

So many people use echo() ; and print() ; functions interchangeably, what are the differences between the two functions?
Re: Can You Solve PHP Problems? Let's Find Out.. by toheebDOTcom(m): 2:41pm On Oct 08, 2016
DanielTheGeek:
Next question:

So many people use echo() ; and print() ; functions interchangeably, what are the differences between the two functions?

echo can output multiple statements, print cannot

echo 'yes', 'no', $some_variable;

print 'yes', 'no', $some_variable; //error
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 2:46pm On Oct 08, 2016
toheebDOTcom:


echo can output multiple statements, print cannot

echo 'yes', 'no', $some_variable;

print 'yes', 'no', $some_variable; //error

Correct bro. You've got 10 points.

Don't expect the next question to be easy..
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 3:00pm On Oct 08, 2016
The explanation is what I'm looking for in this question, so the person with the best explanation towards the behaviour of the code below gets the points.


$foo = “bar”;
$foo++;
echo $foo;

What will the above code snippet echo out and why exactly?
Re: Can You Solve PHP Problems? Let's Find Out.. by toheebDOTcom(m): 3:44pm On Oct 08, 2016
^^^
Let's do it this way...

++ is a step incrementor...

$foo++ means output the value of $foo then assign a step increment of the value of $foo to $foo...

So echo $foo; // 'bar'


However, if question were to be ++$foo, it will increment it first before outputting the result...
so echo $foo; // 'bas'

cc: danielthegeek
Re: Can You Solve PHP Problems? Let's Find Out.. by Kodejuice: 3:47pm On Oct 08, 2016
DanielTheGeek:
The explanation is what I'm looking for in this question, so the person with the best explanation towards the behaviour of the code below gets the points.



What will the above code snippet echo out and why exactly?

"bas" - i dont think this needs an explanation, the last character in the string was incremented. if the string "123" was incremented, the output would be "124". ($var++ !== $var+1)
Re: Can You Solve PHP Problems? Let's Find Out.. by toheebDOTcom(m): 3:50pm On Oct 08, 2016
^^^kodejuice
$foo++ returns the value before incrementing.
++$foo increments before returning the value...

2 Likes

Re: Can You Solve PHP Problems? Let's Find Out.. by FrankLampard: 3:55pm On Oct 08, 2016
Use any language to solve this very simple.

Question.

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 3:56pm On Oct 08, 2016
toheebDOTcom:
^^^
Let's do it this way...

++ is a step incrementor...

$foo++ means output the value of $foo then assign a step increment of the value of $foo to $foo...

So echo $foo; // 'bar'


However, if question were to be ++$foo, it will increment it first before outputting the result...
so echo $foo; // 'bas'

cc: danielthegeek
Your output is unsurprisingly correct, but I don't think the explanation Is okay.

Why is the string being incremented like a number?
Everyone will expect, $num=1; $num++ ; to return a greater number surely, but for strings...how does it work?

(1) (2) (Reply)

Django Programmers Thread / Please What Are The Benefits Of Learning Php And Mysql. Please I Need Advice / Python Django Android...

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 34
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.