This question already has answers here:
The 3 different equals
(5 answers)
Closed 3 years ago.
Summary: Trying to add +1 each time I go through my foreach loop, but it keeps going up even though I would only expect a maximum of 11 results in the loop
I tried to move around on the $i++; but without luck, I also check that I made sure the $i = 0; was outside of the foreach loop.
<?php
$facade = new NewsArticleFacade();
$news = $facade->getAll();
unset($facade);
$i = 0;
foreach($news as $new)
{
$i++;
if ($i = 5000000000000000)
{
echo "if statement: " . $i;
break;
}
$content = substr($new->getContent(), 0, 69);
echo "<div class='news'>";
echo "<h4><a href='/news/news.php?id=".$new->getNewsID()."'>".$new->getTitle()."</a></h4>";
echo "<span>by <a href='#'>".$new->getUser()->getUsername()."</a> - ".$new->getDate()->format("d-M-Y")."</span>";
echo "<p>".$content." ...</p>";
echo "<div class='gradient'></div>";
echo "<img src='img/news/0000000001.jpg'>";
echo "</div>";
}
?>
What really confuses me is if I remove the if statement for the loop, and just echo $i each time, it will echo out 11 in total, what am i doing wrong with my if?
I would expect the loop to go through 11 times since I have 11 entries in the database for this selection, but it keeps going up, if I echo within the if statement i can see the value will just be whatever i limit the if statement to be.
Thanks in advance!
This
if ($i = 5000000000000000)
Is assignment and it makes little sense as such in your code. You probably wanted to have comparison operation which is expressed with ==
if ($i == 5000000000000000)
Related
This question already has answers here:
What is the most efficient way to count all the occurrences of a specific character in a PHP string?
(4 answers)
Closed 5 years ago.
I am trying to separate 2 symbols from a string and then
count how many of these symbols there are.
So if i had : 1110001110000
Then it should find that there are 6= 1's and 7= 0's
So This is what I have tried:
Essentially what I need, is to read the string indexes in code would be string[$i] then IF there is a 1 or a 0 count it.
I tried using a for-loop
for ($i=0; $i < $getInput[$i] ; $i++) {
if ($getInput[$i] == 1) {
echo "ONE";
} elseif ($getInput[$i] == 0) {
echo "ZERO";
}
}
Here im trying to echo out ONE for everytime ther is a 1 and ZERO for everytime theres a zero.
$counter = 0;
foreach ($getInput as $key) {
echo $key;
}
here i tried to utilize a foreach, here I am not really declaring to see for One index, i tried putting a for each in a for but needless to say, it didn't work.
Using substr_count, you can do this in a fairly straightforward way:
echo substr_count("1110001110000", '1'); //Echos 6
echo substr_count("1110001110000", '0'); //Echos 7
Substr_count.
http://php.net/manual/en/function.substr-count.php
$str = "1110001110000";
Echo "there is " .substr_count($str, "0") ." zeros \n";
Echo "there is " .substr_count($str, "1") ." ones \n";
https://3v4l.org/BQEiR
If you want to output it as your code implies (one one one zero) you can use numberformatter.
Here I split the string to an array and loop through it and output the spellout of each number.
$str = "1110001110000";
$arr = str_split($str);
$nf = new NumberFormatter("en", NumberFormatter::SPELLOUT);
Foreach($arr as $numb){
echo $nf->format($numb) . " ";
}
Output:
one one one zero zero zero one one one zero zero zero zero
https://3v4l.org/nHX59
This question already has answers here:
How exactly does if($variable) work? [duplicate]
(8 answers)
Closed 7 years ago.
This is the code
<p>We are going to flip a coin until we get three heads in a row!</p>
<?php
$headCount = 0;
$flipCount = 0;
while ($headCount < 3) {
$flip = rand(0, 1);
$flipCount ++;
if ($flip) {
$headCount ++;
echo "<div class=\"coin\">H</div>";
} else {
$headCount = 0;
echo "<div class=\"coin\">T</div>";
}
}
echo "<p>It took {$flipCount} flips!</p>";
?>
I've played around with Java but I'm now learning PHP. The part that doesn't make sense right here is this.
if ($flip) {
$headCount ++;
echo "<div class=\"coin\">H</div>";
}
What exactly is being checked here? I doubt it's checking whether $flip is true.
It's a common shorthand in these kinds of languages. Once you get used to it, it seems natural.
In this case,
if ($flip)
is equivalent to:
if ($flip == 1)
But PHP is really just checking for a "truthy" value: true, not zero, not "0" or "", not [], and others will evaluate as true and pass the if ($flip) test.
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 7 years ago.
I would like to Increment numbers with double digits if the number is less then 10
This is what i tried so far
$i = 1;
echo $i++;
results is 1,2,3,4,5,6 so on
Then i try adding a condition
$i = 1;
if ($i++<10){
echo "0".$i++;
}else{
echo $i++;
}
Work but skipping the numbers 2,4,6,8 so on.
Can anyone tell me the proper way to do this?
If the condition is only there for the leading zero you can do this much easier with this:
<?php
$i = 10;
printf("%02d", $i++);
?>
if you want prepend something to a string use:
echo str_pad($input, 2, "0", STR_PAD_LEFT); //see detailed information http://php.net/manual/en/function.str-pad.php
On the second fragment of code you are incrementing $i twice, that's why you get only even numbers.
Incrementing a number is one thing, rendering it using a specific format is another thing. Don't mix them.
Keep it simple:
// Increment $i
$i ++;
// Format it for display
if ($i < 10) {
$text = '0'.$i; // Prepend values smaller than 10 with a zero
} else {
$text = $i;
}
// Display it
echo($text);
<?php
$i = 1;
for($i=1;$i<15;){
if($i<10){
echo '0'.$i++."<br>";
}else{
echo $i++."<br>";
}
}
?>
This question already has answers here:
PHP loop: Add a div around every three items syntax [duplicate]
(4 answers)
Closed 9 years ago.
I want to wrap every 10-groups of items in a while-loop inside a wrapper.
Visualized:
echo "<ul class='wrapper'>";
while(get_field('items'))
{
echo "<li>item</li>";
}
echo "</ul>";
In this case every element would be inside this one wrapper, but I have to wrap at max ten elements and then start a new wrapper.
What would be the best way to accomplish this?
You can try this
$count=1;
echo "<ul class='wrapper'>";
while(get_field('items'))
{
if($count % 10 == 0) {echo '</ul><ul class='wrapper'>';}
echo "<li>item</li>";
$count++;
}
echo "</ul>";
A different way to do this is:
<?php
$count = 0;
$group = array();
while(get_field('items'))
{
array_push($group, "<li>$val</li>");
if(++$count % 10 == 0)
{
echo "<ul class='wrapper'>".implode("", $group)."</ul>";
$group = array();
}
}
?>
This question already has answers here:
make string of N characters
(5 answers)
Closed 2 years ago.
I'm creating quite a complex html <table> layout and at this early stage it quite time consuming for me to copy and paste each <tr> in order to generate dummy content.
My idea was to specify a dummy <tr> as a $var and then output that x number of times using a function as below:
$html = "<tr>//content</tr>";
function dummy_html($html, $times){
$i = 0;
for ($i <= $times) {
echo $html;
$i = $i++;
}
}
echo dummy_html($html, 5);
But this is returning a parse error on the for line any idea why that might be ?
PHP has a function already
echo str_repeat($html,5);
Your for loop is incorrect. It should be something like:
for( $i = 0; $i <= $times; $i++ ) {
echo $html;
}
Update
#Your Common Sense's solution is better: str_repeat (http://php.net/manual/en/function.str-repeat.php)
http://php.net/manual/en/control-structures.for.php
for should use the notation: for (set arguments, conditions, command to run at the end of the loop), therefor should be:
for($i = 0; $i <= $times; $i++)
Also, I would recommend using str_repeat (http://php.net/manual/en/function.str-repeat.php)