PHP Hello World Program - php

Hi I'm making a quick PHP program that takes in an integer (x) and prints the numbers from 1 to that number.
If the number is divisible by 3 then print "Hello".
If the number is divisible by 7 print "World".
If the number is divisible by 3 & 7, print "Hello World".
The output is nothing.
$var = 0;
if (isset($_POST['submit']))
{
for ($i = 1; $i < $var; $i++)
{
if ($var % 3 == 0)
{
echo 'Hello' . "\n";
}
if ($var % 7 == 0)
{
echo 'World'. "\n";
}
if (($var % 7 == 0) && ($var % 3 == 0))
{
echo 'Hello World'. "\n";
} else {
echo "";
}
}
}

If you've posted the correct code (meaning without typo), that's perfectly normal the output is nothing.
Look at what you wrote:
$var = 0;
Then you do
for ($i = 1; $i < $var; $i++) {}
No wonder why nothing is output. It is exactly the same as doing
for ($i = 1; $i < 0; $i++) {}
$i is never < to 0. So the for loop is never launched.
You need to set $var to store the user input from your form submission.

Your $var is always = to 0.
And you should test if ($var % 7 == 0) && ($var % 3 == 0) at first
and add else if. Because here if ($var % 7 == 0) && ($var % 3 == 0) the output will be Hello World Hello World. All the if will be executed.
Here the correct code :
if (isset($_POST['submit']))
{
$var = $_POST['var'];
for ($i = 1; $i < $var; $i++)
{
if (($var % 7 == 0) && ($var % 3 == 0))
{
echo 'Hello World'. "\n";
}
else if ($var % 3 == 0)
{
echo 'Hello' . "\n";
}
else if ($var % 7 == 0)
{
echo 'World'. "\n";
}
else {
echo "Nothing";
}
}
}

Related

Count how many times something was echoed in a range?

I'm just a little stumped, I have a simple range of 1-20 listed out, and displays as follows, showing multiples of 3 within a range, and multiples of 5 within a range. These were displayed with echoes, is there a possible way that I can just count the number of times a specific multiple was displayed/echoed? for instance this is what I have:
1, 2, 3foo, 4, 5bar, 6foo, 7, 8, 9foo, 10bar, 11, 12foo, 13, 14, 15bar, 16, 17, 18foo, 19, 20bar
but I would like it to show a count like
foo: 6 times listed
bar: 4 times listed
Does anyone know of a possible way to use echo substr_count for just words "foo" and "bar" that have been echoed within a range of 1-20?
Let me know if I can clarify. Any guidance would be greatly appreciated!
This is my code:
<?php
foreach (range(1, 20) as $number ) {
echo $number;
echo ' ';
if ($number % 3 == 0 && $number %5 == 0) {
echo "foobar ";
} elseif ($number % 3 == 0) {
echo "foo ";
} elseif ($number % 5 == 0) {
echo "bar ";
}
}
echo "<br>";
ob_start();
// code that prints all the numbers
$output = ob_get_flush();
$foo_count = substr_count($output, "foo");
$bar_count = substr_count($output, "bar");
echo "foo: $foo_count times listed<br>";
echo "bar: $bar_count times listed<br>";
?>
Use the output buffering functions to capture echoed output into a variable.
ob_start();
foreach (range(1, 20) as $number ) {
echo $number;
echo ' ';
if ($number % 3 == 0 && $number %5 == 0) {
echo "foobar ";
} elseif ($number % 3 == 0) {
echo "foo ";
} elseif ($number % 5 == 0) {
echo "bar ";
}
}
$output = ob_get_flush();
$foo_count = substr_count($output, "foo");
$bar_count = substr_count($output, "bar");
echo "foo: $foo_count times listed<br>";
echo "bar: $bar_count times listed<br>";
But doing it this way is silly, you can just increment the counters in the loop:
$foo_count = $bar_count = 0;
foreach (range(1, 20) as $number ) {
echo $number;
echo ' ';
if ($number % 3 == 0 && $number %5 == 0) {
echo "foobar ";
$foo_count++;
$bar_count++;
} elseif ($number % 3 == 0) {
echo "foo ";
$foo_count++;
} elseif ($number % 5 == 0) {
echo "bar ";
$bar_count++;
}
}
DEMO

A Logical Algorithm In PHP

I'm a beginner learning PHP. I have tried to make a loop that has a different behaviour for both even and odd numbers. I've been playing around with it for a while, yet I still can't get it to work. Has anyone got a solution?
$count = 0;
$mod = $count % 2;
while ($count < 10)
{
if ($mod == 0) {
echo "even, ";
} else {
echo "odd, ";
}
$count++;
}
A silly mistake, mod inside while() loop.
$count = 0;
while ($count < 10) {
$mod = $count % 2; //Here
if ($mod == 0) {
echo "even, ";
} else {
echo "odd, ";
}
$count++;
}
$count = 0;
$mod = $count %2;
Is were your problem is.
You have to use the modulus (%) operator inside the for loop. Also, there is no need to store the value from the use of the modulus operator at all, it can be compared directly inside the for-loop.
for ($count = 0; $count < 10, $count++) {
if ($count % 2 == 0) {
echo "even, ";
} else {
echo "odd, ";
}
}
You can also switch the while to a for like this.
Welcome to PHP.
Edit #1:
As you are getting a new value of $count every execution of the for-loop the old value if $count % 2 will be incorrect. It has to recalculate for every $count. First it checks if 0 is divisible by 2, then onto 1 and so forth. For every value of $count you have to check the divisibility.
In most programming languages you aren't computing a variable onto another, instead you are taking the value of the variable. Like $a = $b + $c; in that case, if you change the value of $b or $c it does not automatically update $a. Instead you have to call $a = $b + $c again. It is the same with % operator.
$count = 0;
while ($count < 10) {
$mod = $count % 2;
if ($mod == 0) {
echo "even, ";
} else {
echo "odd, ";
}
$count++;
}
use for loop instead of while loop
for($count=0;$count<10;$count++)
{
if(($count % 2) == 0)
echo "even,";
else
echo "odd,";
}

Count elements in a loop

I'm trying to count elements in a loop to break each number of elements and show in groups
My little script
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter = explode("*", trim($data));
for ($x = 0; $x <= count($exp_filter); $x++)
{
print "".$exp_filter[$x]."";
if ($x%5 == 0)
{
print "<br>";
}
}
As you can see in the little script each 5 rounds I want show the tag for break and show as in groups of elements.
The problem it´s always show in the first line one element and after this the rest, and no works fine.
The index of $exp_filter starts at 0, so this block of code
if ($x % 5 == 0)
{
print "<br>";
}
should be
if (($x+1) % 5 == 0)
{
print "<br>";
}
Here's the complete modified code
$data = "house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter = explode("*", trim($data));
for ($x = 0; $x <= count($exp_filter); $x++)
{
print "".$exp_filter[$x]."";
if (($x + 1) % 5 == 0)
{
print "<br>";
}
}
Working example: http://codepad.org/iEsKK98M
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter=explode("*",trim($data));
for($x=1;$x<=count($exp_filter);$x++)
{
print "".$exp_filter[$x]."";
if($x%5==0)
{
print "<br>";
}
}
Try this. The problem is you started at 0 in for, you should start from 1 ;)
Quickfix:
Demo
<?php
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter=explode("*",trim($data));
for($x=1;$x<=count($exp_filter);$x++)
{
print "".$exp_filter[$x]."";
if($x > 0 && $x%5==0)
{
echo "<br />";
}
}
?>
I would use array chunk and implode instead:
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
foreach(array_chunk(explode('*', $data), 5) as $chunk){
echo implode(' ', $chunk) . '<br>';
}
Live example: http://codepad.viper-7.com/ED2wHR

PHP - If number is divisible by 3 and 5 then echo

I'm new to PHP and trying to create the following whilst minimizing the amount of code needed. PHP should show a list of 100 then display if the number is / by 3, 5 or 3 and 5. If not by any then show nothing.
This is what I've got so far, but any help would be great since not sure about the / by 3 and 5 bit as you can see below.
<?php $var = range(0, 100); ?>
<table>
<?php foreach ($var as &$number) {
echo " <tr>
<td>$number</td>
<td>";
if($number % 3 == 0) {
echo "BY3";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 and 5 == 0) {
echo "BY3 AND 5";
}
echo "</td></tr>";
}
?>
</table>
Thanks
Nope... you should check first if it's divisble for 15 (3x5) (or 3 and 5) and after you can do other checks:
if($number % 15 == 0) {
echo "BY3 AND 5";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 == 0) {
echo "BY3";
}
echo "</td></tr>";
?>
Because every number divisble for 15 is also divisble for 3 and 5. So your last check could never hit
if I'm reading your question correct then you are looking for :
if ($number % 3 == 0 && $number %5 == 0) {
echo "BY3 AND 5";
} elseif ($number % 3 == 0) {
echo "BY3";
} elseif ($number % 5 == 0) {
echo "BY5";
}
Alternative version :
echo ($number % 3 ? ($number % 5 ? "BY3 and 5" : "BY 3") : ($number % 5 ? "BY 5" : ""));
$num_count = 100;
$div_3 = "Divisible by 3";
$div_5 = "Divisible by 5";
$div_both = "Divisible by 3 and 5";
$not_div = "Not Divisible by 3 or 5";
for($i=0;$i<=$num_count;$i++)
{
switch($i)
{
case ($i%15==0):
echo $i." (".$div_both.")</br>";
break;
case ($i%3==0):
echo $i." (".$div_3.")</br>";
break;
case ($i%5==0):
echo $i." (".$div_5.")</br>";
break;
default:
echo $i."</br>";
break;
}
}
No need to do three if statements:
echo "<table border='1'>";
for ($i = 1; $i <= 100; $i++) {
echo "<tr><td>{$i}</td><td>";
if ($i % 3 == 0) echo "BY3 ";
if ($i % 5 == 0) echo "BY5";
echo "</td></tr>\n";
}
echo "</table>";
Update the code as given below
<?php $var = range(0, 100); ?>
<table>
<?php foreach ($var as &$number)
{
echo " <tr>
<td>$number</td>
<td>";
if($number % 3 == 0 && $number % 5 == 0)
{
echo "BY3 AND 5";
}
elseif ($number % 5 == 0)
{
echo "BY5";
}
elseif ($number % 3 == 0)
{
echo "BY3";
}
echo "</td></tr>";
}
?>
<?php
if($number % 5 == 0 && $number % 3 == 0) {
echo "BY3 AND 5";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 == 0) {
echo "BY3";
} else{
echo "NOT BY3 OR 5";
}
?>
if($number % 15 == 0)
{
echo "Divisible by 3 and 5";
}
elseif ($number % 5 == 0)
{
echo "Divisible by 5";
}
elseif ($number % 3 == 0)
{
echo "Divisible by 3";
}
This is neater and completed to be run:
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i % 15 == 0)
{
echo"Divisible by 3 and 5</br>";
}
elseif ($i%3==0)
{
echo"Divisible by 3</br>";
}
elseif ($i%5==0)
{
echo"Divisible by 5</br>";
}
else
{
echo $i,"</br>";
}
}
?>
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i % 15 == 0) echo "This Number is Divisible by 3 and 5<br>";
else if ($i % 3 == 0) echo "This Number is Divisible by 3 only<br>";
else if ($i % 5 == 0) echo "This number is Divisible by 5 only<br>";
else{
echo "$i<br>";
}
}
?>

PHP Conditional > how to know what didn't match?

if i have statement:
$a = 1;
$b = 2;
$c = 3;
if($a == 1 && $b == 2 && $c == 3)
{
echo 'correct';
}
else
{
echo 'what variable's weren't matched';
}
Is there any way of knowing what didn't watch instead of writing everything separately?
Cheers!
No. Your expression was turned into a boolean, so apart from checking the equality(s) again you cannot find out which triggered the "false".
You need to test each individually, but you could do something like this:
$a = 1;
$b = 2;
$c = 3;
$a_matched = $a == 1;
$b_matched = $b == 1;
$c_matched = $c == 1;
if($a_matched && $b_matched && $c_matched)
{
echo 'correct';
}
else
{
if (!$a_matched) echo 'a did not match!';
if (!$b_matched) echo 'b did not match!';
if (!$c_matched) echo 'c did not match!';
}
but that's less clear than just:
$a = 1;
$b = 2;
$c = 3;
if($a == 1 && $b == 2 && $c == 3)
{
echo 'correct';
}
else
{
if (!$a == 1) echo 'a did not match!';
if (!$b == 2) echo 'c did not match!';
if (!$c == 3) echo 'b did not match!';
}
Actually, heh, I take back my comment. You can rely on the boolean short-circuiting to set a variable indicating the last part of the conditional which was true:
if (($x = 'a') && $a == 1 && ($x = 'b') && $b == 2 && ($x = 'c') && $c == 3) {
echo "correct\n";
} else {
echo "$x is wrong\n";
}
Note, I would never write this in production code because it's goofy and very hard to understand what's supposed to be going on. But fun to fiddle with, at least.
Nope! That's not possible. You can make life a lot simpler by using arrays, though:
$results = array(1, 2, 4);
$expected = array(1, 2, 3);
$count = count($results);
$wrong = array();
for($i = 0; $i < $count; $i++) {
if($results[$i] !== $expected[$i]) {
$wrong[] = $i;
}
}
if(count($wrong) > 0) {
echo "There were wrong ones. They were at positions: " . implode(', ', $wrong);
} else {
echo "All good!";
}
For example.

Categories