Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,743 members, 7,817,054 topics. Date: Saturday, 04 May 2024 at 01:22 AM

PHP Simple Puzzle - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / PHP Simple Puzzle (3755 Views)

Web Masters Please Help Me Solve This Puzzle / I Challenge The Level Of Intelligence On This Forum With This Puzzle. (2) (3) (4)

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

PHP Simple Puzzle by omni1: 1:55pm On Apr 06, 2013
Is 15,000 same as 15000?

Prove using code samples.

(NB: Not for advanced coders. Allow the upcoming ones get better.)
PS: Don't claim advanced when you don't even know anything.

1 Like

Re: PHP Simple Puzzle by ogzille(m): 3:06pm On Apr 06, 2013
Bros, I can answer shebi? grin
Re: PHP Simple Puzzle by Nobody: 4:16pm On Apr 06, 2013
<?php
$string = "15,000";
$int = 15000;

echo $string *2; /// 30
echo "<br />";
echo $int *2;
/// 30000
?>

no
Re: PHP Simple Puzzle by babihouse(m): 10:10pm On Apr 06, 2013
NO
<?php
$v = 15,000;
$p = 15000;

echo $v; $p;
?>
output
15,000 ; 1500

i am a learner, i just felt like trying
Re: PHP Simple Puzzle by miracle4(m): 12:47pm On Apr 07, 2013
ActiveMan:
<?php
$string = "15,000";
$int = 15000;

echo $string *2; /// 30
echo "<br />";
echo $int *2;
/// 30000
?>

no
Re: PHP Simple Puzzle by omni1: 6:08pm On Apr 07, 2013
ogzille: Bros, I can answer shebi? grin
If I catch you say pim . . . . Waddup?
Re: PHP Simple Puzzle by omni1: 3:33pm On Apr 08, 2013
ediko5: Please How can i get information posted by my site users on my site(for instance the contact form) either through email or any any other available method.
This is off topic please. Create a new thread for it please.

ActiveMan:
<?php
$string = "15,000";
$int = 15000;

echo $string *2; /// 30
echo "<br />";
echo $int *2;
/// 30000
?>

no
Instead of multiplying, you can simple just compare the two variables. You already understood the difference that 15000 is an INTEGER while 15,000 is a mixture of NUMBERS & a CHARACTER. A simple code could be

echo (15000 > '15,000') ? true : false;


If the output is 1 then it's true while if 0 it's false.

You can play around with this to see more examples.

$a = "15,000";
$b = "1,500,000";

babihouse: NO
<?php
$v = 15,000;
$p = 15000;

echo $v; $p;
?>
output
15,000 ; 1500

i am a learner, i just felt like trying
Nice try. Keep attempting and you'll get used to it along the line.
Re: PHP Simple Puzzle by Nobody: 4:11pm On Apr 08, 2013
*omni:

This is off topic please. Create a new thread for it please.


Instead of multiplying, you can simple just compare the two variables. You already understood the difference that 15000 is an INTEGER while 15,000 is a mixture of NUMBERS & a CHARACTER. A simple code could be

echo (15000 > '15,000') ? true : false;


If the output is 1 then it's true while if 0 it's false.

You can play around with this to see more examples.

$a = "15,000";
$b = "1,500,000";


Nice try. Keep attempting and you'll get used to it along the line.

if/else statement can solve this bro

echo (15000 > '15,000') ? which return a Boolean also solve the puzzle without real explanation
Re: PHP Simple Puzzle by omni1: 4:20pm On Apr 08, 2013
@ActiveMan
echo (15000 > '15,000') ? true : false;

^^^ Is an IF/ELSE statement. Just call it Tenary operator.

http://php.net/manual/en/language.operators.comparison.php
Re: PHP Simple Puzzle by Nobody: 4:48pm On Apr 08, 2013
*omni:
@ActiveMan
echo (15000 > '15,000') ? true : false;

^^^ Is an IF/ELSE statement. Just call it Tenary operator.

http://php.net/manual/en/language.operators.comparison.php
lol grin grin grin
Re: PHP Simple Puzzle by Nobody: 8:22pm On Apr 11, 2013
Lets keep it simple.
<?php
$a = 15000;
$b = "15,000";
//calling the function variable dump
var_dump($a);
var_dump($b);
?>


RESULT
int 15000

string '15,000' (length=6)
Re: PHP Simple Puzzle by Nobody: 11:57pm On Apr 11, 2013
New Puzzle

$a = 1;
$b = "1";

Prove the following statements
$a == $b;
$a !=== $b;


Use code samples.
Re: PHP Simple Puzzle by Gerardcole(m): 11:20am On Apr 12, 2013
Can explain in words:

$a == $b; //$a and $b are equal. True.

$a !=== $b; //$a and $b are not identical. True

E.g:

<?php
$a = 0;
$b = 000;
if($a == $b)
{
echo '$a and $b are equal';
} else {
echo '$a and $b are not equal';
}
echo '<br>';
if($a === $b)
{
echo '$a and $b are identical';
} else {
echo '$a and $b are not identical';
}
?>

The code above will output:

$a and $b are equal
$a and $b are not identical

1 Like

Re: PHP Simple Puzzle by Nobody: 2:39pm On Apr 13, 2013
Geraldcole.Perfect solution.Please use comments so beginner can follow.
Simply put
In Boolean Operations,

Statement 1 below evaluates to true.if (case 1) x=1 and y=1.0 and also where (case 2) $x=1 and $y= "1".

Note that in case 1 variable x is an integer and variable y is a float.while in case 2 y is a string.(These are all data types in PHP).That means value and data type may vary and Statement 1 will still be valid.(PHP Switching)
For Statement 2,value and type must be the same for both variables to be equal.Hence $x is not equal to $y.

$x == $y
$x !=== $y


I have received many requests from people who want to learn PHP Programming.

This is a good avenue to learn.So lets keep this thread alive.Ask any questions if you re confused at any point.will be posting more puzzles.Cheers.

1 Like

Re: PHP Simple Puzzle by omni1: 3:02pm On Apr 13, 2013
Web Contractor: Geraldcole.perfect solution.use comments so beginner can follow.

I have received many requests from people who want to learn PHP Programming.

This is a good avenue to learn.So lets keep this thread alive.Ask any questions if you re confused at any point.will be posting more puzzles.Cheers.
Thanks so much for loving to help the beginners. Let's write more codes for them so they can learn from. Issue is many are scared of writing the wrong codes and getting corrected but the truth is, when you write them poorly or wrongly, others put you in the right direction. Your coding structure and actual codes improve. Let's keep it alive here.

More confusions, puzzles or questions coming. I'll post mine here as well for help.

Cheers!
Re: PHP Simple Puzzle by Nobody: 3:06pm On Apr 13, 2013

NEW PUZZLE
Given the array below,write a PHP statement that returns all the values of the array.

<?php

$even_numbers = array(2,4,6,8,10,12);

?>


Difficulty Level: Beginner
Hint:Use Loops
Re: PHP Simple Puzzle by Nobody: 3:10pm On Apr 13, 2013
*omni:

Thanks so much for loving to help the beginners. Let's write more codes for them so they can learn from. Issue is many are scared of writing the wrong codes and getting corrected but the truth is, when you write them poorly or wrongly, others put you in the right direction. Your coding structure and actual codes improve. Let's keep it alive here.

More confusions, puzzles or questions coming. I'll post mine here as well for help.

Cheers!

Thanks man.Let's keep the thread alive.Am available for anyone that is willing to learn.

1 Like

Re: PHP Simple Puzzle by Snippets(m): 4:16pm On Apr 13, 2013
@ WebContractor - Can U Please gimme links as to where I can get php based puzzles that I can solve and improve my coding experience.
Thanks
Re: PHP Simple Puzzle by Nobody: 5:21pm On Apr 13, 2013
Snippets: @ WebContractor - Can U Please gimme links as to where I can get php based puzzles that I can solve and improve my coding experience.
Thanks

I don't have any links to puzzles.I put together the ones u saw.I will be posting more.

Waiting for responses on the current puzzle.You can improve your coding experience here.

Just answer the puzzle and I will post another ASAP.
Re: PHP Simple Puzzle by Gerardcole(m): 7:39pm On Apr 13, 2013
I will attack this puzzle in two ways for beginners purpose.

1. Saving values to variable.

<?php
/* I'm typing on my phone so I will use short variable names. Hope you'll undertand */

echo 'Example 1<br>';

$ev = array('2', '4', '6', '8', '10', '12');

/* We are gonna use a foreach() loop which processes every member of an array.*/

$new = ''; //sets an empty variable we will fill our values to.

foreach{$ev as $a)
{
$new .= $a.'<br>';
/* Notice the concatenation operator or you call it dot in Nigeria. I mean '.' It adds the string on the right after the equal sign to the already existing string on the left. Without it, the $new will be replaced by the value on the right */
}

/*Now our values are stored in the variable $new
When we gt to the line we wanna use it, we print*/

echo $new;
?>

2. Outputting variables directly.
<?php

echo 'Example 2';

$ev = array('2', '4', '6', '8', '10', '12');

/* Its still the same foreach() function */

foreach($ev as $a)
{
echo $a . '<br>';
}
?>


The above examples would output

Example 1
2
4
6
8
10
12

Example 2
2
4
6
8
10
12



Notice the dot before the <br> in the source code of examples 1 and 2. This tells php that this variable or string ends here, the following is another.

Since PHP doesn't understand white-spaces when its not appearing within quotes, dots serve as comma in English language.
Re: PHP Simple Puzzle by Nobody: 11:09pm On Apr 13, 2013
Gerardcole:

2. Outputting variables directly.
<?php

echo 'Example 2';

$ev = array('2', '4', '6', '8', '10', '12');

/* Its still the same foreach() function */

foreach($ev as $a)
{
echo $a . '<br>';
}
?>

I must say,you attacked it nicely.Just a little explanation for learners to follow.

Foreach loops can only be used with arrays.
it has the format

<?php
foreach($array as $value){
statement;
}
?>

Foreach loop will go through every single element of the array($array) and each of the elements(2,4,6,8,10,12) will become the value($value) which can then be referenced in the statement.

Simply put,we are pulling each of the elements in the array and assigning them to a variable name temporarily, and then executing a statement on them.
Re: PHP Simple Puzzle by Nobody: 11:26pm On Apr 13, 2013
NEW PUZZLE
Given the array below,write a PHP conditional statement that returns 2,4,8,10

<?php

$even_numbers = array(2,4,6,8,10,12);

?>;


Difficulty Level: Intermediate
Hint:Use Loops

Edit: Remove the last Comma from your result
Re: PHP Simple Puzzle by Nobody: 12:22am On Apr 14, 2013
<?php
$out = '12';
$even_numbers = array('2','4','6','8','10','12');

foreach($even_numbers as $number) {
if($number !== $out) {
echo $num.","; } }
?>
Re: PHP Simple Puzzle by Gerardcole(m): 5:57am On Apr 14, 2013
In addition to what Judinho59 wrote.

You can use an exclamation mark '!' to mean not in php. Just like Judinho59 used:

$number != 12;
/*Which translates to: if the current number is not equal to 12 */

Also note that in PHP, '!=' and '!==' are the same.

You can use the greater than '>' or lesser than '<' symbol. It works the same way it does in Mathematics.

From Judinho59's example,

if($number < 12) { /* Meaning: if $numbet is lesser than 12*/ }

This will output same values as Judinho59's. But in case the members of the int array has greater numbers than 12 and you want no values from 12 above, you can use this.

Incase you were wondering, Judinho59's code will output.

2,4,6,8,10,

I would love a puzzle on removing the last comma after 10 as it is not prof. to leave it there.
Re: PHP Simple Puzzle by Nobody: 7:06am On Apr 14, 2013
Gerardcole:
I would love a puzzle on removing the last comma after 10 as it is not prof. to leave it there.

Ah my bad.The last comma has got to go.Puzzle still on!
Re: PHP Simple Puzzle by Nobody: 7:08am On Apr 14, 2013
Judinho59:
<?php
$out = '12';
$even_numbers = array('2','4','6','8','10','12');

foreach($even_numbers as $number) {
if($number !== $out) {
echo $num.","; } }
?>

Good Job.But Please remove the last comma.Also from the array,you are to return 2,4,8,10 only.So Take away 6 from your result
Re: PHP Simple Puzzle by Gerardcole(m): 8:37am On Apr 14, 2013
Okay. Since according to our puzzle provider, Judinho59 didn't get the required output, I ll provide my own solution. The puzzle is kinda tricky.

Given array('2', '4', '6', '8', '10', '12')
To return 2,4,8,10 (Removing 6 and 12 and the last comma that's supposed to appear after 10)

I'm also gonna attack in two methods.

1. Using PHP in_array()

<php
/* First we set the values we do not want in an array */
$unwanted = array('6', '12');

/*Then we set a variable which will help in placing comma's */

$counter = 0;

/*Then to the code */
$ev = array('2', '4', '6', '8', '10', '12');

foreach($ev as $a)
{
if(!in_array($unwanted, $a)
/*Checks if $a is not in our unwanted list before proceeding*/
{
if($counter > 0)
/* Since we don't want it after our last value but after every other, we run this before the value. > 0 here explains that we don't want it before the first as our counter is still 0 before this line */
{
echo ', ';
}
echo $a;
$counter++;
/*In PHP ++ is same as +1 so whenever it gets to the line above, the counter is increased buly 1*/
}

}
?>

2. Using the modulus operator '%'

The modulus %, in PHP returns the remainder in a division operation. E.g 7 % 5 returns 2 (i.e 1 remaining 2), 10 % 5 returns 0 (i.e 2 remaining 0).

I wouldn't be explaining the lines I already explained in example 1 above.

<php
$div = 6;
/* Since all the unwanted numbers are divisible by 6. */

$counter = 0; //As explained in e.g 1 above

$ev = array('2', '4', '6', '8', '10', '12');

foreach($ev as $a)
{
if($a % $div != 0)
/*Since it will return 0 for our unwanted 6 and 12*/
{
if($counter > 0)
{
echo ', '; //As explained in e.g 1 above
}
echo $a
$counter++; //As explained in e.g 1 above.
}
}
?>


The above returns:
/*From E.g 1 */

2, 4, 8, 10

/*From e.g 2 */

2, 4, 6, 10
Re: PHP Simple Puzzle by Nobody: 9:47am On Apr 14, 2013
Web Contractor:

Good Job.But Please remove the last comma.Also from the array,you are to return 2,4,8,10 only.So Take away 6 from your result

Thanks. I didnt see 6 missing out.

@Gerardcole: Nice Explanation there.. cheesy

<?php
$out = array('6','12');
$even_numbers = array(2,4,6,8,10,12);
$result = '';

foreach($even_numbers as $number) {

if(!in_array($number, $out)) {
$result .= $num.',';
}
}
echo rtrim($result, ',');
Re: PHP Simple Puzzle by Nobody: 1:52pm On Apr 14, 2013
Gerardcole: Okay. Since according to our puzzle provider, Judinho59 didn't get the required output, I ll provide my own solution. The puzzle is kinda tricky.

Given array('2', '4', '6', '8', '10', '12')
To return 2,4,8,10 (Removing 6 and 12 and the last comma that's supposed to appear after 10)

I'm also gonna attack in two methods.

1. Using PHP in_array()

<php
/* First we set the values we do not want in an array */
$unwanted = array('6', '12');

/*Then we set a variable which will help in placing comma's */

$counter = 0;

/*Then to the code */
$ev = array('2', '4', '6', '8', '10', '12');

foreach($ev as $a)
{
if(!in_array($unwanted, $a)
/*Checks if $a is not in our unwanted list before proceeding*/
{
if($counter > 0)
/* Since we don't want it after our last value but after every other, we run this before the value. > 0 here explains that we don't want it before the first as our counter is still 0 before this line */
{
echo ', ';
}
echo $a;
$counter++;
/*In PHP ++ is same as +1 so whenever it gets to the line above, the counter is increased buly 1*/
}

}
?>

2. Using the modulus operator '%'

The modulus %, in PHP returns the remainder in a division operation. E.g 7 % 5 returns 2 (i.e 1 remaining 2), 10 % 5 returns 0 (i.e 2 remaining 0).

I wouldn't be explaining the lines I already explained in example 1 above.

<php
$div = 6;
/* Since all the unwanted numbers are divisible by 6. */

$counter = 0; //As explained in e.g 1 above

$ev = array('2', '4', '6', '8', '10', '12');

foreach($ev as $a)
{
if($a % $div != 0)
/*Since it will return 0 for our unwanted 6 and 12*/
{
if($counter > 0)
{
echo ', '; //As explained in e.g 1 above
}
echo $a
$counter++; //As explained in e.g 1 above.
}
}
?>


The above returns:
/*From E.g 1 */

2, 4, 8, 10

/*From e.g 2 */

2, 4, 6, 10

I have to disagree with you even though your concept is right.Your codes generate syntax errors and I do not have extra time to debug.Please test and refine.Also use the # to enclose your codes.It makes it more readable.I like your commenting style!

1 Like

Re: PHP Simple Puzzle by Nobody: 1:56pm On Apr 14, 2013
Judinho59:

Thanks. I didnt see 6 missing out.

@Gerardcole: Nice Explanation there.. cheesy

<?php
$out = array('6','12');
$even_numbers = array(2,4,6,8,10,12);
$result = '';

foreach($even_numbers as $number) {

if(!in_array($number, $out)) {
$result .= $num.',';
}
}
echo rtrim($result, ',');

Am getting errors when i run this script.Please test and refine.

(1) (2) (3) (Reply)

How To Remove Powered By Word Press And Theme Designer From Your Blog: / How To Increase Your Adsense Earnings / How To Improve Your Blog Revenue

(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. 57
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.