printf inside echo [duplicate] - php

This question already has answers here:
Why does echo combined with printf displays wrong output? [duplicate]
(3 answers)
How to concatenate strings with function calls while using echo?
(6 answers)
Closed 11 months ago.
How do you insert a printf comment inside of an echo? I have been trying to figure it out for the past hour and everything I try results in a different error.
Below is the last version of the code that I tried.
<?php
$i = 0;
do {
echo "<li><a class=\"th radius\" href=\"img/coolstuff/" . printf("%03d", $i); . ".jpg\"><img src=\"img/coolstuff/" . printf("%03d", $i); . ".jpg\"></a></li>";
$i++;
} while ($i < 282);
?>

Remove the ; before the ., otherwise you're terminating the statement.
Additionally, printf directly outputs the string, which will make it appear at the start of the outputted string. You want sprintf instead.

Use sprintf() instead. It returns the formatted string instead of outputting it.
Also, as #Kolink mentioned, remove the semicolons, so your echo would look like this:
echo "<li><a class=\"th radius\" href=\"img/coolstuff/" . sprintf("%03d", $i) . ".jpg\"><img src=\"img/coolstuff/" . sprintf("%03d", $i) . ".jpg\"></a></li>";

Do like this:
echo "<li><a class=\"th radius\" href=\"img/coolstuff/" , printf("%03d", $i);
Notice the comma , instead of dot ..
When using a comma with a echo statement, like above, each part is evaluated first. Atleast according to this accepted anwer.

Related

How Do I Count Up By Two In PHP? [duplicate]

This question already has answers here:
Adding 2 to a variable each time
(5 answers)
Closed 1 year ago.
So I'm trying to make it so the computer counts up to ten by two and making a new line each time. When I tested this my entire computer crashed. What's wrong with it?
<?php
$test = 0;
while ($test < 10) {
$test + 2;
echo $test . "/n";
}
?>
Change:
$test + 2;
to:
$test += 2;
Currently, $test is always 0 in your code, since you are not assigning a new value to it, hence the infinite loop.
Additionally, you would also want to change:
echo $test . "/n";
to:
echo $test . "\n";
Since you need to use a backslash to indicate a new line character.
N.B. In PHP you can also use the PHP_EOL constant to indicate the end of a line.
Here you just sum $test with 2 but did not assign it back to $test
$test + 2;
Trying replace it by $test += 2;
Another point is /n is not "making a new line" as you expected, change it to \n instead.
Although you can use while statement, this is a good case to use for instead.
for($i=0; $i < 10; $i += 2)
{
echo $i . PHP_EOL;
}
Usually while is used to evaluate something every loop, some variable that can change inside the while, like a bool variable or an "infinite" loop until break.
When you have a already defined number of loops, for is usually a better approach.

add numbers to new lines separated by comma [duplicate]

This question already has answers here:
PHP counter increment in a while loop
(5 answers)
Closed 2 years ago.
i am having some issues with my php code. what i wish to add numbers like before every lines something like below
1. some data
2. some more data
what i have tried doing is like below but it would not multiply the numbers like i want it to, it only prints 1
$num = 1;
$tdetails = $num++. str_replace(',', '<br />', $row['travellers_details']);
Could someone please show how to achieve what i am looking for?
thanks
You need to iterate and increment num each time. You can store the elements in an array first using explode:
$num = 1;
$tdetails = explode(',', 'some data, some more data');
$str = "";
for($i = 0; $i < count($tdetails); $i++)
$str .= $num++ . ". ". $tdetails[$i] . "<br>";
echo $str;
Output:
1. some data
2. some more data
You can make use of HTML ol(ordered list) tag which will automatically do the numbering for you.
<?php
$tdetails = "<ol><li>" . implode("</li><li>", explode(",",$row['travellers_details'])) . "</li></ol>";
echo $tdetails;

auto increment value in foreach loop, but it keeps going [duplicate]

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)

how to read for a certain symbol in a string [duplicate]

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

Incrementing numbers with double digits in php [duplicate]

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

Categories