How to echoing the variable and loop? - php

I want to make in html page like this below:
But, I don't want to write them all one by one, because the number(1, 2, 3) are from $number variable and it can be more than 3.

Use a for loop:
for ($i = 1; $i <= $number; $i++) {
echo "<a href='$i'";
if ($i == $currentvid) {
echo " class='currentvid'";
}
echo "></a>";
}

Try this:
<?php
$href="1";
for($i=1;$i<4;$i++){
if($href==$i){
echo "$i";
}else{
echo "$i";
}
}
?>

$i = 1;
foreach ($number as $k)
{
if($i == 1)
{
echo "$i";
}
else
{
echo "$i";
}
}
}

Related

Duplicating or Replicating pattern for php

I would like to duplicate the pattern, the function will have 2 parameters, which is row and column. It will duplicate the pattern on how many rows and columns. My function for pattern is correct but how can I duplicate/replicate it. Should I make it in array? Thanks
<?php
$row_col_array = array();
function honey(){
$l = "";
for($i=2; $i<=4; $i++)
{
for($j=4; $j>=2; $j--)
{
if($i == $j)
echo "*";
else
echo " ";
}
for($k=2; $k<=4; $k++)
{
if($i == $k)
echo "*";
else
echo " ";
}
echo "<br>";
}
for($i=4; $i>=2; $i--)
{
for($j=4; $j>=2; $j--)
{
if($i == $j)
echo "*";
else
echo " ";
}
for($k=2; $k<=4; $k++)
{
if($i == $k)
echo "*";
else
echo " ";
}
echo "<br>";
}
}
honey();
?>

I Want to print Star pattern

I want to print star pattern like :
below is my code. but it doesn't work for me. How can i fix this problem.
for($i=1; $i<=5; $i++){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
for($i=4; $i>=1; $i--){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
?>
You should put all text that should keep form and not whitespace trimmed in between a <pre/> tag
<pre><?php
for($i=1; $i<=5; $i++){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
for($i=4; $i>=1; $i--){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
} ?></pre>
Note: if your target is console and PHP-CLI, don't put the <pre/> tag, omit the end of PHP tag (?>) and replace all <br/> tags with \n.
Thanks to all of you guys. I add <pre> tag before code and also change <br> tag with \n and now it is work. Thanks again. Have a nice day guys..
Try this...
function printPattern($n)
{
$k = 0;
// Print upper triangle
for ($i = 1; $i <= $n; $i++)
{
// Print spaces
for ($j = 1; $j <= $n - $i; $j++)
{
echo " ";
}
// Print #
while ($k != (2 * $i - 1))
{
if ($k == 0 or $k == 2 * $i - 2)
echo "*";
else
echo " ";
$k++;
}
$k = 0;
// move to next row
echo "\n";
}
$n--;
// Print lower triangle
for ($i = $n; $i >= 1; $i--)
{
// Print spaces
for ($j = 0; $j <= $n - $i; $j++)
{
echo " ";
}
// Print #
$k = 0;
while ($k != (2 * $i - 1))
{
if ($k == 0 or $k == 2 * $i - 2)
echo "*";
else
echo " ";
$k++;
}
echo "\n";
}
}
// Driver Code
$n = 6;
printPattern($n);
// This Code is contributed by mits
?>

Each 3rd Increment Loop PHP

Hi im looking for a way to breakout of a loop every 3 increments to echo a static string.this script echos a "test" after each increment. i want a test after each 3rd increment. any ideas ?
my loop:
<?php
$i = 0;
while (++$i < 100){
$x = $i - 3;
if ($i+3) {echo $i . "<br>TEST<br>";}
else{ echo $i . "<br>";}
}
?>
try below Code
<?php
for($i=1;$i<=100;$i++)
{
if($i%3==0)
{
echo $i."test"."<br>";
}
}
?>
use % operator in if loop,
i = 0;
while (++$i < 100){
$x = $i - 3;
if ($i%3 == 0) {echo $i . "<br>TEST<br>";}
else{ echo $i . "<br>";}
}

Sum of Prime numbers

I have the following code, to output all prime numbers from array. I would like to get the sum of the output in ex: 2+3+5 = 10, Any hint how to get that ?
$n = array(1,2,3,4,5,6);
function prime($n){
for($i=0;$i<= count($n);$i++){
$counter = 0;
for($j=1;$j<=$i;$j++){
if($i % $j==0){
$counter++;
}
}
if($counter == 2){
print $i."<br/>";
}
}
}
print prime($n);
Then this should work for you:
(Here i used $sum which i initialized before the foreach loop and then used the += operator to add the sum together)
<?php
$n = array(1,2,3,4,5,6);
function prime($n){
$sum = 0;
foreach($n as $k => $v) {
$counter = 0;
for($j = 1; $j <= $v; $j++) {
if($v % $j == 0)
$counter++;
}
if($counter == 2) {
echo $v."<br/>";
$sum += $v;
}
}
echo "Sum: " . $sum;
}
prime($n);
?>
Output:
2
3
5
Sum: 10
As #IMSoP commented above, one option is to compile the list of primes into a new array:
$m = [];
// looping code...
// If prime:
array_push( $m, $primeNumber );
Then, when you're done, you can do your printing mechanism:
print implode( "<br />", $m );
And then you can do your summing mechanism:
print "<p>Sum: " . array_sum( $m ) . "</p>";
The added benefit here is you can split out each piece of functionality into it's own function or method (which you should do to have a good design).
try this
<?php
define('N', 200);
function isPrime($num)
{
if ($num == 2 || $num == 3) { return 1; }
if (!($num%2) || $num<1) { return 0; }
for ($n = 3; $n <= $num/2; $n += 2) {
if (!($num%$n)) {
return 0;
}
}
return 1;
}
for ($i = 2; $i <= N; $i++) {
if (isPrime($i)) {
$sum += $i;
}
}
echo $sum;
You can try something like this:
function isPrime($n){
if($n == 1) return false;
if($n == 2) return true;
for($x = 2; $x <= sqrt($n); $x++){
if($n % $x == 0) return false;
}
return true;
}
$sum = 0;
$n = array(1,2,3,4,5,6);
foreach($n as $val){
if(isPrime($val)) {
echo $val . "<br />";
$sum += $val;
}
}
echo "Sum: " . $sum;
<?php
$num = 100;
for($j=2;$j<$num;$j++)
{
for($k=2;$k<$j;$k++)
{
if($j%$k==0)
{
break;
}
}
if($k==$j)
{
$prime_no[]=$j;
}
}
echo "<pre>";
print_r($prime_no);
echo "</pre>";
for($j=0;$j<count($prime_no);$j++)
{
$myprimeAdd = $prime_no[$j] + $prime_no[$j+1];
if(in_array($myprimeAdd,$prime_no))
{
echo "Resultant Prime No:-", $myprimeAdd;
echo nl2br("\n");
break;
}
}
?>

PHP Table Loop?

<?php
$i=0;
while($i < 101){
if($i%2==0){
echo "<tr>".PHP_EOL;
}
echo "<td>".$i."</td>".PHP_EOL;
$i++;
if($i%2==0){
echo "</tr>".PHP_EOL;
}
}
?>
This code generates a table with 100 rows and 2 columns. But what I want to do is that show ordered numbers (upp to 100) in the left side of the rowcells and show something else (ex. pow(rownumber) ) in the right side of the rowcells. How can I do that?
Try this, Will output 100 rows with the number and its power in two columns
<table>
<?php
for($i = 0; $i <= 100; $i++){
echo sprintf('<tr><td>%s</td><td>%s</td></tr>',
$i,
pow($i, 2)
);
}
?>
</table>
Is this what you are looking for?
for($i=0; i<100; i++){
if($i%2==0){
echo "<tr>".PHP_EOL;
}
echo "<td>".$i."</td>".PHP_EOL;
echo "<td>Something Else</td>".PHP_EOL;
if($i%2==0){
echo "</tr>".PHP_EOL;
}
}
You can use your modulus (and a for loop too)
for ($i = 1; $i <= 100; $i++){
$mod = ($i%2==0) ? true : false;
if($mod) echo "<tr>".PHP_EOL;
echo "<td>".$i."</td>".PHP_EOL;
echo "<td>". foo($bar) ."</td>".PHP_EOL;
if($mod) echo "</tr>".PHP_EOL;
}
<?php
$i=0;
$j=0;
while($i < 101){
if($i%2==0){
$j++;
echo "<tr>"."<td>".$j."</td>".PHP_EOL;
}
echo "<td>".$i."</td>".PHP_EOL;
$i++;
if($i%2==0){
echo "<td>any text</td>"."</tr>".PHP_EOL;
}
}
?>
Why use modulo for only two options? This solution seems much easier. Your $data is an array of the things you want to display, currently the alphabet.
$data = range('a','z');
foreach($data as $num => $elem) {
echo "<tr>".PHP_EOL;
echo "<td>".$num.</td>".PHP_EOL;
echo "<td>".$elem.</td>".PHP_EOL;
echo "</tr>".PHP_EOL;
}
If you want to make it loop 100+ times, just make the array that size.
Foreach documentation

Categories