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

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>";
}
}
?>

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

Printing star pattern in alphabetical shape but not working

hii i'm trying to improve logic in php where i'm trying to print star pattern in A shape,B shape and K shape but it isnt working can anyone help me with my logic my code is as follows
k.php
<?php
$j = 5;
$i = 0;
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == $column + 1) and $column != 0))
echo "*";
else if ($row == $i and $column == $j)
{
echo "*";
$i=$i+1;
$j=$j-1;
}
else
echo " ";
}
echo "<br>";
}
?>
B.php
<?php
for ($row=0; $row<7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == 0 or $row == 3 or $row == 6) and ($column < 5 and $column > 2)) or ($column == 5 and ($row != 0 and $row != 3 and $row != 6)))
echo "*";
else
echo " ";
}
echo "<br>";
}
?>
A.php
<?php
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ((($column == 1 or $column == 5) and $row != 0) or (($row == 0 or $row == 3) and ($column > 1 and $column < 5)))
echo "*";
else
echo " ";
}
echo "<br>";
}
?>
my code isnt working as expected please anyone help
You can try this code:
<pre><?php echo getK();?></pre>
<pre><?php echo getA();?></pre>
<pre><?php echo getB();?></pre>
<?php
function getK()
{
$j = 5;
$i = 0;
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == $column + 1) and $column != 0))
echo "*";
else if ($row == $i and $column == $j)
{
echo "*";
$i=$i+1;
$j=$j-1;
}
else
echo " ";
}
echo "\n";
}
}
function getA()
{
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ((($column == 1 or $column == 5) and $row != 0) or (($row == 0 or $row == 3) and ($column > 1 and $column < 5)))
echo "*";
else
echo " ";
}
echo "\n";
}
}
function getB()
{
for ($row=0; $row<7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == 0 or $row == 3 or $row == 6) and ($column < 5 and $column > 2)) or ($column == 5 and ($row != 0 and $row != 3 and $row != 6)))
echo "*";
else
echo " ";
}
echo "<br>";
}
}
One more answer:
legend:
n = "new line (\n)"
* = letter (you can use, bettor for you)
space = space)))
for example: "a" => " *** n
* *n
* *n
*****n
* *n
* *n
* *",
Code:
<?php
$letters = [
"a" => " *** n* *n* *n*****n* *n* *n* *",
"b" => "**** n* *n* *n****n* *n* *n**** ",
"k" => "* *n* *n* *n**n* *n* *n* *",
];
function printLetter($pattern)
{
$len = strlen($pattern);
for ($i = 0; $i < $len; $i++) {
if ($pattern[$i] == 'n') {
echo "\n";
continue;
}
echo $pattern[$i];
}
}
?>
<pre><?php printLetter($letters['a']); ?></pre>
<pre><?php printLetter($letters['b']); ?></pre>
<pre><?php printLetter($letters['k']); ?></pre>

Displaying retrieve data in different table

Okay, what I want is to display retrieved data in a table form database, I want my table to be limit in 5 data per table. I need the tables to be display horizontally.
Like this:
while($row1 = sqlFetchArray($row))
{ $ctr = 0;
$ctr+1;
if($ctr=1 || $ctr<=5)
{
$html .='<table width="100px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=6 || $ctr<=10)
{
$html .='<table width="110px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=11 || $ctr<=15)
{
$html .='<table width="120px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
}
Output:
1 6
2 7
3 8
4 9
5 10
This should work for you:
(But the function sqlFetchArray as to work like mysql_fetch_array)
<?php
$limit = 15;
for($numberCounter = 0; $numberCounter < $numberCount = mysql_num_rows($row); $numberCounter++) {
$count = 0;
if($numberCounter >= mysql_num_rows($row))
break;
if ($count == 0 || $count % $limit == 0)
echo "<table width='100px' style='float:left;'>";
while ($row1 = sqlFetchArray($row) && $count < $limit) {
if($numberCounter >= mysql_num_rows($row))
break;
echo "<tr><td>" . $row1[$numberCounter] . "</td></tr>";
$count++;
$numberCounter++;
}
if($count == 0 || $count % $limit == 0)
echo "</table>";
}
?>
As an example:
<?php
$test = range(1, 43);
$limit = 15;
for($numberCounter = 0; $numberCounter < $numberCount = count($test); $numberCounter++) {
$count = 0;
if($numberCounter >= count($test))
break;
if ($count == 0 || $count % $limit == 0)
echo "<table width='100px' style='float:left;'>";
while ($count < $limit) {
if($numberCounter >= count($test))
break;
echo "<tr><td>" . $test[$numberCounter] . "</td></tr>";
$count++;
$numberCounter++;
}
if($count == 0 || $count % $limit == 0)
echo "</table>";
}
?>

Prime numbers up to a certain number

here is the problem, i need to find prime numbers up to a certain number and here is my code:
$a = 56;
for($i = 2; $i<=$a; $i++)
{
if($i == 2)
{
echo "2</br>";
}
if($i == 3)
{
echo "3</br>";
}
if($i == 5)
{
echo "5</br>";
}
if($i == 7)
{
echo "7</br>";
}
for($j =3; $j <= ceil($i/2); $j = $j + 2)
{
if($i % 2 == 0)
{
break;
}
if($i % 3 == 0)
{
break;
}
if($i % 5 == 0)
{
break;
}
if($i % 7 == 0)
{
break;
}
else
{
echo "$i</br>";
break;
}
}
}
It works fine, but it kinda seems like a brute force algorithm, doesnt it? Is there any other way to do this?
Thanks for help!!!
Suppose x is the limit (till which you want prime number)..
for($n=2;$n<=$x;$n++)
{
$i=2;
while($i<=$n-1)
{
if($n % $i == 0)
break;
$i++;
}
if($i==$num)
echo $n; // $n is the prime number...
}

Break mysql_num_rows into 6x4

Pretty much I have a code like below that I'm trying to add </tr><tr> to after every 6 results.
echo "<table><tr>";
$query="SELECT * WHERE id='$id' ORDER BY date ASC";
$result=mysql_query($query);
if (mysql_num_rows($result) > 0) {
while($rts = mysql_fetch_array($result)){
$cdata1 = $rts['cdata1'];
$cdata2 = $rts['cdata2'];
echo "<td>$cdata1 and $cdata2</td>";
}
}else{
echo "<td>no results</td>";
}
echo "</tr></table>";
echo "<table><tr>";
$query="SELECT * WHERE id='$id' ORDER BY date ASC";
$result=mysql_query($query);
$i = 0;
if (mysql_num_rows($result) > 0) {
while($rts = mysql_fetch_array($result)){
$cdata1 = $rts['cdata1'];
$cdata2 = $rts['cdata2'];
echo "<td>$cdata1 and $cdata2</td>";
if(++$i % 6 == 0) {
echo '</tr><tr>';
}
}
}else{
echo "<td>no results</td>";
}
echo "</tr></table>";
UPD:
Whats means if(++$i % 6 == 0) code:
++$i equals $i = $i + 1;
$i % 6 means $i modulo 6
If $i modulo 6 equals 0 then echo </tr><tr>
So we can write it as:
$i = $i + 1;
if($i % 6 == 0) {
echo '</tr><tr>';
}
http://php.net/manual/en/internals2.opcodes.mod.php
http://php.net/manual/en/language.operators.increment.php

Categories