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
Related
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";
}
}
}
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
can any body help me how I can delete the white space at the end of the output.
This might be easy question but to be honest it took me more than 45 minutes and still nothing. Assignment
Write a PHP script that prints the numbers from the number inputted on form to zero. The numbers should be separated with a single space but the last number, zero, should not have a space after it. If the user inputs a number smaller than zero, print “The number should be at least zero!” The used form looks like this:
Luku:
Example output
5 4 3 2 1 0
my code:
<?php
$number=$_GET["number"];
if ($number < 0){
echo "The number should be at least zero!";
} else {
for($i=$number; $i>=0; $i=$i-1)
{
echo $i." ";
}
}
?>
You can use trim()
<?php
$number=$_GET["number"];
if ($number < 0){
echo "The number should be at least zero!";
} else {
$num = '';
for($i=$number; $i>=0; $i=$i-1)
{
$num .= $i." ";
}
echo trim($num);
}
?>
Try this:
echo implode(" ",range($number,0));
Magic and trickery ;)
You can use rtrim instead of trim to delete white spaces at the end of the string:
<?php
$number=$_GET["number"];
if ($number < 0){
echo "The number should be at least zero!";
} else {
for($i=$number; $i>=0; $i=$i-1)
{
$num .= $i." ";
}
}
$num = rtrim($num);
?>
You could use an if-statement:
<?php
$number=$_GET["number"];
if ($number < 0) {
echo "The number should be at least zero!";
} else {
for($i=$number; $i>=0; $i=$i-1)
{
echo $i . ($i != 0 ? " " : "");
}
}
Or you use trim:
<?php
$number=$_GET["number"];
$output = "";
if ($number < 0) {
echo "The number should be at least zero!";
} else {
for($i=$number; $i>=0; $i=$i-1)
{
$output .= $i . " ";
}
}
echo trim($output);
try this:
<?php
$number=$_GET["number"];
if ($number < 0) {
echo "The number should be at least zero!";
} else {
$string = $number;
for($i=$number-1; $i>=0; $i=$i-1)
{
$string .= " $i";
}
}
echo $string;
?>
I have a value as a number. For instance, 502.
I want to write a php if statement that will display some text if the value is lesser or greater than certain numbers, or between a range.
E.g.
number is 502, text will say: "Between 500-600"
number is 56, text will say: "Between 0-60"
etc.
So far I have this:
<?php $count=0;?>
<?php $board = getUserBoard($userDetails['userId']);?>
<?php if(is_array($board)):?>
<?php $boardCount = count($board);?>
<?php foreach($board as $key=>$value):?>
<?php
$boardPin = getEachBoardPins($value->id);
$count = $count + count($boardPin);
?>
<?php endforeach?>
<?php endif?>
And that gives me a number:
<?php echo $count;?>
I have tried writing...
<?php if(($count)): => 500 ?>
Over 500
<?php endif ?>
But I keep running into errors.
I'd like to create a list if possible with elseif statements denoting various number ranges.
E.g.
0-50, 51-250, 251-500 etc.
Can anyone help me?
Thanks.
The sanest, neatest and most widely used syntax for if conditions in PHP is:
if($value >=500 && $value <=600 )
{
echo "value is between 500 and 600";
}
if ($count >= 0 && $count < 100) {
echo 'between 0 et 99';
} elseif ($count < 199) {
echo 'between 100 and 199';
} elseif { ...
}elseif ($count < 599) {
echo 'between 500 and 599';
} else {
echo 'greater or equal than 600';
}
I wrote something like this a few years back (might be a better way to do it):
function create_range($p_num, $p_group = 1000) {
$i = 0;
while($p_num >= $i) {
$i += $p_group;
}
$i -= $p_group;
return $i . '-' . ($i + $p_group - 1);
}
print 'The number is between ' . create_range(502, 100) . '.';
It'll say 500-599, but you can adjust it to your needs.
I'm not sure what you need, but here is what I understand you ask:
function getRange($n, $limit = array(50, 250, 500)) { // Will create the ranges 0-50, 51-250, 251-500 and 500-infinity
$previousLimit = 0;
foreach ($limits as $limit) {
if ($n < $limit) {
return 'Between ' . ($previousLimit + 1) . ' and ' . $limit; //Return whatever you need.
}
$previousLimit = $limit;
}
return 'Greater than ' . $previousLimit; // Return whatever you need.
}
echo getRange(56); // Prints "Between 51 and 250"
echo getRange(501); // Prints "Greater than 500"
echo getRange(12, array(5, 10, 15, 20)); // Prints "Between 11 and 15"
function getRange($number){
$length=strlen($number);
$length--;
$r1=round($number,-$length);
if ($r1>$number){
$r2=$r1-pow(10,$length);
return ''.$number.' value is between '.$r2.'-'.$r1;
}
else {
$r2=$r1+pow(10,$length);
return ''.$number.' value is between '.$r1.'-'.$r2;
}
}
Try this.
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>";
}
}
?>