<?php
$numbers = array("12", "-32", "52", "-65", "98");
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
How can i add + sign where is not - minus.
Result:
+12,
-32,
+52,
-65,
+98
you can add condition like this:
echo (int)$numbers[$x] > 0 ? '+'.$numbers[$x] : $numbers[$x];
so it will be
$numbers = array("12", "-32", "52", "-65", "98");
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo ((int)$numbers[$x] > 0) ? '+'.$numbers[$x] : $numbers[$x];
echo "<br>";
}
For a very straight forwards echo modification, replace
echo $numbers[$x];
with
echo ( $numbers[$x] > 0 ) ? '+'.$numbers[$x] : $numbers[$x];
Related
I want to print number from 1 to 12 in matrix form and expected output is:
1 5 9
2 6 10
3 7 11
4 8 12
code:
<?php
for ($i=1; $i<=12; $i++)
{
for($j=1;$j<=$i;$j++)
{
echo $i." ";
}
echo "<br/>";
}
?>
I have got wrong output. So, How can I get expected output as I mention above? Please help me.
Thank You
Some "magic" code):
foreach (range(1,4) as $num) {
echo implode(' ', range($num,12,4)) . '<br />';
}
Version with for:
for ($i = 1; $i <= 4; $i++) {
for ($j = $i; $j <= 12; $j +=4) {
echo $j . ' ';
}
echo '<br />';
}
$z=0;
for ($x = 1; $x <= 4; $x++)
{
echo " $x ";
$z=$x;
for ($y = 1; $y <= 2; $y++)
{
$z=$z+4;
echo " $z ";
}
echo "\n";
}
$maxRow = 4;
$maxColumn = 3;
for ($row = 1; $row <= $maxRow; $row++)
{
for ($column = 1; $column <= $maxColumn; $column++) {
$number = $row + 4*($column-1);
echo $number." ";
}
echo "<br/>";
}
should work
foreach (range(1, 4) as $res) {
echo implode(' ', range($res, 12, 4));
echo "<br>";
}
I have to make a diamond-shaped asterisk using for loop, inside a table. It has to have "blank" <td> spaces before and after the asterisks to move and make it look centered, so it looks like a diamond. How do I do that? (I used PHP inside an HTML code.)
Code without the <tr> and <td> tags, it looked like a diamond because it was center aligned:
<center>
<?php
echo "<table border = 1>";
// loop for the pyramid
for($i = 1; $i <= 10; $i += 2) {
for($j = 1; $j <= $i; $j++) {
echo "* ";
}
echo "<br />";
}
// loop for the inverted pyramid, so it looks like a diamond
for($i = 7; $i >= 1; $i -= 2) {
for($j = 1; $j <= $i; $j++) {
echo "* ";
}
echo "<br />";
}
echo "</table>";
?>
</center>
Code with the <tr> and <td> tags, need "spaces" for it to look like it's center aligned:
<?php
echo "<table border = 1>";
// loop for the pyramid
echo "<tr>";
for($i = 1; $i <= 10; $i += 2) {
echo "<tr>";
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
echo "</tr>";
}
echo "</tr>";
// loop for the inverted pyramid, so it looks like a diamond
for($i = 7; $i >= 1; $i -= 2) {
echo "<tr>";
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
echo "<br />";
echo "</tr>";
}
echo "</table>";
?>
Please help!
Here is new Code with your solution. I have added logic to put blank td forward and backward to *
<?php
echo "<table border = 1>";
// loop for the pyramid
echo "<tr>";
$max = $initAmount = 10;
for($i = 1; $i <= $initAmount; $i += 2) {
$max = $max -2;
$halfTD = (int)$max/2;
echo "<tr>";
for($b = 1; $b <= $halfTD; $b++){
echo "<td></td>";
}
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
for($b = 1; $b <= $halfTD; $b++){
echo "<td></td>";
}
echo "</tr>";
}
echo "</tr>";
// loop for the inverted pyramid, so it looks like a diamond
$max = $initAmount = 10;
for($i = 7; $i >= 1; $i -= 2) {
$max = $max -2;
$diff = $initAmount - $max;
$blankTd = $diff/2;
echo "<tr>";
for($b = 1 ; $b <= $blankTd; $b++){
echo "<td></td>";
}
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
for($b = 1 ; $b <= $blankTd; $b++){
echo "<td></td>";
}
echo "</tr>";
}
echo "</table>";
?>
I used the code below without using a table to make a diamond shape.
<div style="text-align: center">
<?php
$n = 8;
if($n === 1){ die("input must be greater than 1"); }
$nn = ($n * 2);
$m = (ceil($nn / 2) + 1);
$temp = 0;
for($x = 1; $x <= $nn; $x++){
$temp = (($x < $m) ? ($temp + 1) : ($temp - 1));
$total = ($temp > 1 ? ((2 * $temp) - 1) : $temp);
echo nl2br(str_repeat('* ', $total) . "\r\n");
}
?>
I used the code below.
<div style="text-align: center">
<?php
$n = 8;
if($n === 1){ die("input must be greater than 1"); }
$nn = ($n * 2);
$m = (ceil($nn / 2) + 1);
$temp = 0;
for($x = 1; $x <= $nn; $x++){
$temp = (($x < $m) ? ($temp + 1) : ($temp - 1));
$total = ($temp > 1 ? ((2 * $temp) - 1) : $temp);
echo nl2br(str_repeat('* ', $total) . "\r\n");
}
?>
<?php
for($i=0;$i<=5;$i++){
for($j=5;$j>=$i;$j--){
echo ' ';
}
for($k=0;$k<=$i;$k++){
echo '*';
}
echo '<br />';
}
for($i=0;$i<=4;$i++){
for($k=0;$k<=$i+1;$k++){
echo ' ';
}
for($j=4;$j>=$i;$j--){
echo '*';
}
echo '<br />';
}
?>
this is what i want.
123456
23456
3456
456
56
6
Hi, i have trouble with this loop.
<?php
for ($x = 7; $x >= 1; $x--) {
for ($y = 7; $y > $x; $y--) {
echo "  ";
}
$s = 7;
while ($s < $x) {
$f++;
$s--;
}
for ($f=1; $f < 7; $f++) {
echo "$f";
}
echo "<br>";
}
?>
this is what i got. I want to get the $f work but it is ignoring it.
You can make it simpler than you did.
for($x = 1; $x <= 6; $x++) {
for($y = 1; $y <=6; $y++){
if($x > $y)
echo "  ";
else
echo $y;
}
echo "<br>";
}
With x you control the lines and with y the columns. If the lines is greater than the column you print the spaces, and if not, the number.
Please help
when i want yo remove an element from array buu after when i print array it shows error
4
6
2
NOTICE Undefined offset: 3 on line number 16
<?php
$value = 22;
$numbers = array(4, 6, 2, 22, 11);
$arrlength = count($numbers);
for($y = 0; $y < $arrlength ; $y++) {
if ( $numbers[$y] == $value) {
unset($numbers[$y]);
}
}
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>
You should use "array_values()" to re-index the array. You can find the explication here : http://php.net/manual/en/language.types.array.php#language.types.array.useful-funcs
Use array_values().
<?php
$value = 22;
$numbers = array(4, 6, 2, 22, 11);
$arrlength = count($numbers);
for($y = 0; $y < $arrlength ; $y++) {
if ( $numbers[$y] == $value) {
unset($numbers[$y]);
}
}
$numbers = array_values($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>
Check Online Demo : Click Here
OR
<?php
$value = 22;
$numbers = array(4, 6, 2, 22, 11);
$arrlength = count($numbers);
for($y = 0; $y < $arrlength ; $y++) {
if ( $numbers[$y] == $value) {
unset($numbers[$y]);
}
}
//$numbers = array_values($numbers);
//$arrlength = count($numbers);
foreach($numbers as $key=>$num) {
echo $num;
echo "<br>";
}
?>
you are trying to print element of not existing key
try this:
<?php
$value = 22;
$numbers = array(4, 6, 2, 22, 11);
$arrlength = count($numbers);
for($y = 0; $y < $arrlength ; $y++) {
if ( $numbers[$y] == $value) {
unset($numbers[$y]);
}
}
foreach($numbers as $k => $number)
echo "key: ". $k . " value: ". $number . "<br />";
I have a foreach to run
for($i = 0; $i < 7; $i++){
echo "<div class='$_Vr'">" ,$i,"</div>" <p> </p>;
}
and have a test value
if ($_Vr == 'dd'){
echo 'yes';
}else{
echo 'no';
}
I understand I can do this way to get what I want
<?php
for($i = 0; $i < 7; $i++){
echo "<div class='$_Vr'">" ,$i,"</div><p>"?>
if ($_Vr == 'dd'){
echo 'yes';
}else{
echo 'no';
}
<?php </p>"; }?>
but I'd like to know if I can put another echo inside foreach ? like
for($i = 0; $i < 7; $i++){
echo "<div class='$_Vr'">" ,$i,"</div>" <p> " , test value here , "</p>;
}
Use the conditional (AKA ternary) operator:
for($i = 0; $i < 7; $i++){
echo "<div class='$_Vr'>$i</div> <p> " , ($_Vr == 'dd' ? "yes" : "no") , "</p>;
}
for($i = 0; $i < 7; $i++){
echo "<div class='$_Vr'>$i</div><p>".($_Vr == 'dd' ? 'yes': 'no')."</p>";
}