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)
Related
This question already has answers here:
PHP variable variables
(6 answers)
Closed 3 years ago.
I have the following php code where by a for loop create different variables. Basically what I need is, after run the code below, it should create different variables such as $o1, $o2, $o3 etc. But it is not working based on the way I implemented. Does anyone can help me how to solve this?
for($i = 0; $i <= $totalOa; $i++){
'$o'.$i = $pieces[$i];
}
Try this...
for($i = 0; $i <= $totalOa; $i++){
${"o" . $i} = $pieces[$i];
}
Try this:
You need to use variable variables.
for($i = 0; $i <= $totalOa; $i++){
$o{$i} = $pieces[$i];
}
This question already has answers here:
Get the sum of all digits in a numeric string
(13 answers)
Closed 8 months ago.
I'm trying to adding all numbers from last 6 digit from substr(). Let say the number is 19283774616, I'm trying to have result from this: 7+7+4+6+1+6 = ?. Here is my current code
public function accountHash($accountNumber)
{
$result = 0;
$accountNumber = substr($accountNumber, -6);
for($i=0; $i<=strlen($accountNumber); $i++) {
$result += substr($accountNumber, $i, 1); // A non-numeric value encountered here
}
echo $result;
}
From the function above, "A non-numeric value encountered" error occurred. Need suggestion on how to do this. Thank you
You attempt to get more characters than string contains. Replace "<=" with "<" in your condition expression, i.e. change:
for($i=0; $i<=strlen($accountNumber); $i++) {
to
for($i=0; $i<strlen($accountNumber); $i++) {
You need to use < instead of <= in your for loop.
And you can do it a more simple way,
$result = 0;
for($i = 0; $i < 6; $i++){
$result += $string[-$i];
}
An alternative method without loops (or error checking, for what it's worth):
function accountHash($accountNumber)
{
return array_sum(
preg_split('//u', mb_substr($accountNumber, -6), null, PREG_SPLIT_NO_EMPTY)
);
}
Demo
This question already has answers here:
Can you 'exit' a loop in PHP?
(6 answers)
Closed 4 years ago.
How to prevent for loop from exceeding condition?
Loop below returns 25. I want it to return 20.
for ($i = 5; $sum < 23;)
{
echo $sum += $i;
}
This loop is just an example. There will be variables with any value in place of 5 and 23
You have to write your loop differently.
A for loop is designed to run until the conditions evaluates false. In your case, the loop at sum = 20 will still run, becuase sum < 23 evaluates to true. So if you want 20, simply write $sum < 20.
Or if I give it a second thought, you may want to do it like that:
<?PHP
$sum = 0;
for($i = 5; ($sum+$i) < 23;)
{
$sum += $i;
}
echo $sum;
?>
This question already has answers here:
Can you 'exit' a loop in PHP?
(6 answers)
Closed 4 years ago.
I won't break a loop that is in another loop
these are my codes
for ($i = 0; $i < 20; $i++) {
// some codes
for ($i1 = 0; $i1 < 10; $i1++) {
if (a condition)
{
// i want break this loop not parent loop
}
}
// some codes
}
if I use break; parent loop will break too but I won't break only work for child loop
thanks for your answers
break only breaks out of the loop it's called in. If you want to break out of an outer control structure, you can use the optional integer argument to tell break the number of structures it should break out of (e.g., in this case, break 2 would break out of the outer loop).
I used this and its worked
for ($i = 0; $i < 20; $i++) {
// some codes
for ($i1 = 0; $i1 < 10; $i1++) {
if (a condition)
{
$i1=11;
}
}
// some codes
}
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>";
}
}
?>