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.
Related
I am beginning to learn PHP. Here I am trying to create a loop that checks whether a randomly generated number is 50. If the number is 50, I want to print "it's 50!", if not, I want to echo the result, then repeat the random generation.
Here is my code:
<?php
$number = 0; // sets the number to 0
while ($number != 50) { // I want this loop to repeat when the result isn't 50
echo $number; // print the result
$number = rand(0,100); // reshuffle
if ($number == 50) { // result for if 50 is the result
echo "it's 50!";
}
else { // result otherwise
$number = 0;
}
}
?>
I feel like it's not working because I don't redefine $number based on the result of the rand function, is this correct? If so, how do I redefine the value of the $number variable from within the loop?
The main issue with the code you have submitted is that, if your randomly picked number is not 50 you then assign 0 to it. So your output will be a stream of 0s.
You can modify your code slightly, and drop your else block:
<?php
$number = null;
while ($number != 50) {
$number = rand(0, 100);
echo $number . "\n";
if ($number == 50)
echo "It's 50!";
}
Or, just loop until you strike your number, then break out.
<?php
while (true) {
$number = rand(0, 100);
echo "$number\n";
if ($number == 50) {
echo "it's 50!\n";
break;
}
}
Or even simpler:
while (($number = rand(0, 100)) != 50)
echo "$number\n";
echo "It's 50!";
Let's say I have this loop:
for($i = 1; $i <= 10; $i++){
if($i > 4){
echo 'Greater Than 4.' . '<br/>';
}
}
The previous loop will output the following:
Greater Than 4.
Greater Than 4.
Greater Than 4.
Greater Than 4.
Greater Than 4.
Greater Than 4.
As the condition is true 6 times.
What I want to do is to print this message only once so the output from the same loop and condition will be :
Greater Than 4.
I think this could be achieved by using a variable and give it this value at a specific point , But I don't know how to do it.
Well, it will be like your code expect that you print the message once:
$msg = 'There is no greater than 4';
for($i = 1; $i <= 10; $i++){
if($i > 4){
$msg = 'Greater Than 4.' . '<br/>';
// Make any additional required code here...
}
}
echo $msg;
;
Have you tried this :)
for($i=1;$i<=10;$i++){
echo ($i>4 && !isset($rslt))?$rslt='Greater Than 4.':'';
}
Or, you can loop first
then print the result
like this
$bucket = '';
for($i=1;$i<=10;$i++){
$bucket = ($i>4)?'Greater Than 4.':$bucket;
}
echo $bucket;
Hmmmm, I guess you case like this :D
$bucket = $condition = '';
$flag = 1;
for($i=1;$i<=10;$i++){
if($i>4){
$bucket = 'Greater Than 4. <br>';
$condition .= "Proses :{$flag}<br>";
$flag++;
//more condition process 6 times
}
}
echo $condition;
echo $bucket;
Or like this :D
$j=1;
for($i=1;$i<=10;$i++){
if($i>4){
if($i<=5){
echo 'Greater Than 4. <br>';
}
echo 'Proses '.($j++).'<br>';
//more condition process 6 times
}
}
Or, using the short hand
for($i= $j =0;$i<=10;$i++){
if($i>4){
echo ($i<=5)?'TheGreater Than 4. <br>':false;
$j++;
echo "Process number : {$j} <br>";
}
}
$run_once = true;
for($i = 1; $i <= 10; $i++){
if($i > 4 AND $run_once){
echo 'Greater Than 4.' . '<br/>';
$run_once = false;
}
}
you could also use if ($i == 4)
EDIT:
for($i = 1; $i <= 10; $i++){
if($i > 4){
if($i == 4){
echo 'this runs only once';
}
echo 'this runs every time';
}
}
I have the following bit of code and for some reason in the first WHILE loop the first value of $actor_list (when $i = 0) does not display. If I simply echo $actor_list[0] then it displays fine, but in the loop it will not display. I merely get [td][/td] as the output. The remaining values of the array display fine.
Also the line
echo '</tr><tr> </br>';
is not displaying.
What am I missing here? The value of $num_actors is an even number in my test scenario so there doesn't seem to be a reason for the above echo line to be skipped.
$actor_list = explode(" ", $actors);
$num_actors = count($actor_list);
if ($num_actors <= 6) {
foreach ($actor_list as $actor) {
echo '[td]'.$actor.'[/td] </br>';
}
} elseif ($num_actors <= 12) {
if ($num_actors % 2 == 0) {
$half_actors = $num_actors / 2;
while ($i <= ($half_actors - 1)) {
echo '[td]'.$actor_list[$i].'[/td] </br>';
$i++;
}
echo '</tr><tr> </br>';
while ($i <= $num_actors) {
echo '[td]'.$actor_list[$i].'[/td] </br>';
$i++;
}
}
}
You're not initialising the variable $i to 0, which means it will be set to 'null' and thus not reference the 0th index of the array; but when it's then incremented its value will become 1.
Note: [..] Decrementing NULL values has no effect too, but incrementing them results in 1.
See: http://php.net/manual/en/language.operators.increment.php
Try adding:
$i = 0;
before the if statement.
Made a few changes.
This is working for me.
Try it out.
<?php
$actor_list = 'act1 act2 act3 act4 act5 act6';
$actors = explode(" ", $actor_list);
$num_actors = count($actor_list);
//debug
//echo "<pre>";
//print_r($actor_list);
//echo "</pre>";
echo "<table>";
if ($num_actors <= 6) {
foreach ($actors as $actor) {
echo '<tr><td>'.$actor.'</td></tr>';
}
} elseif ($num_actors <= 12) {
if ($num_actors % 2 == 0) {
$half_actors = $num_actors / 2;
while ($i <= ($half_actors - 1)) {
echo '<tr><td>'.$actor_list[$i].'</td></tr>';
$i++;
}
while ($i <= $num_actors) {
echo '<tr><td>'.$actor_list[$i].'</td></tr>';
$i++;
}
}
}
echo "</table>";
?>
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
I am just a beginner in PHP. I have tried to write a program for prime numbers, but the result is not correct. I couldn't find the error. How can I correct this? Here is my code:
<?php
$n=15;
for($i=2; $i<=$n; $i++)
{
echo "<br />";
for($j=2; $j<=$i-1; $j++)
{
$k=$i%$j;
if($k==0)
{
break;
}
else echo $i."is prime";
break;
}
}
?>
Try this:
<?php
$n=15;
for($i=2; $i<=$n; $i++)
{
$k = 1; //assume that it is prime
for($j=2; $j<$i; $j++) //if $i is 2, then it won't enter the loop as it will not match the condition ($j<$i)
{
$k=$i%$j;
if($k==0)
break; //if not prime, $k will be set as 0. So, break.
}
if($k!=0) // if $k <> 0, then it is prime
echo "<br />" . $i." is prime";
}
?>
Edit
Updated the code to take care of the "2"
Try this:(whitout using loop)
function is_prime($p) {
return ($p > 1) && (($p%2 >= 1) && ($p%3 >= 1) && ($p%5 >= 1)) || in_array($p, [2,3,5]);
}
echo is_prime(15);
You are breaking the loop the first time it's run by calling this basically:
if (something) {
break;
} else {
break;
}
it will break no matter what. you need to take out the last break.
Well, your code is kind of confusing to understand; at first glance it seems as if it's trying to determine primality by checking every divisor up to N, but you've got that outer-loop going on which I didn't see at first... Oh boy.
If you're just trying to figure out if some number N is prime, the following should work:
$n = 15
$prime = true;
for ($i = 2; $i < sqrt($n); $i++) {
if ($n % $i == 0) {
$prime = false;
break;
}
}
echo $n . " is " . ($prime ? "" : "not") . " prime.";
<?php
echo "TEST\r\n";
$n=15;
for($i=2; $i<=$n; $i++)
{
echo "I= $i \r\n";
for($j=2; $j<=$i-1; $j++)
{
$k = $i%$j;
if($k==0)
{
break;
} else {
echo $i."is prime \r\n";
}
break;
}
}
I think your secod for loop is incorrect.
for($i=2; $i<=$n; $i++) {
echo "<br />";
for($j=2; $j<=$i-1; $j++) {
...
The first value for $j is 2. And for the first time, the first value for $i is 2 again. Now, look at your second for loop code.
It will be like this at the first time:
for($j=2; $j<=2-1; $j++) ... // for($j=2; $j<=1; $j++)
And this condition is not valid at all: 2<=1