I'm currently trying out PHP and I'm doing some exercises a friend told me but I can't seem to make the following:
98765
8765
765
65
5
I already made the following code:
<?php
for ($a = 9; $a >= 5; $a–-) {
for ($b = 1; $b <= $a; $b++) {
echo $a;
}
echo "<br>";
}
?>
Any tips / fixes?
Your code should be:
<?php
for($a=9; $a >= 5; $a--) {
for ($b=$a; $b >= 5; $b--) {
echo $b;
}
echo "\n";
}
<?php
function print_triangle($start, $count) {
for ($a = 0; $a < $count; $a++) {
for ($b = $start - $a; $b > $start - $count; $b--) {
echo $b;
}
echo "<br>";
}
}
print_triangle(9, 5);
?>
Hope it helps!
Well, I fixed it by creating the following code:
for ($a = 0; $a <= 9; $a++) {
for ($b = 9 - $a; $b >= 5; $b--) {
echo $b;
}
echo "<br>";
}
?>
Thanks for at least answering guys!
Related
Edit: Solved! Thank you all for pointing out the missing concatenation operator and missing punctuation.
I've just been introduced to PHP and am trying to work on a basic exercise where I use a for-loop in combination with several if/elseif statements.
My variables A and B are related mathematically, and I need the output to state the value of B along with a simple word ('Good', 'bad', 'okay'- depending on the resultant value of B). I'd also like each result of the loop to be printed on a new line.
My attempt can be seen below:
<?php
for ($A = 0; $A <= 100; $A++){
$B = (2/3) * ($A - 25);
if ($B < 0 {
echo $B ", Bad <br>";}
elseif ($B > 45 {
echo $B ", Good <br>";}
else {
echo $B ", Okay <br>";}
}
endfor;
?>
This is only my second day of working with PHP (and the first time I've written a loop statement of any kind in about seven years) so I apologize if my errors are glaring.
Do I need to move my definition of $B? Do I need to split this statement up somehow? Is it my echo statements that are the issue? Any hints or explanations are greatly appreciated. Thanks!
There is some syntax error in your code use . for concatenation in PHP
and there is two types syntax for control structures detail link , either use if( some condition ){ your code } or if(some condition): "your code" endif; .
here is the Difference between if () { } and if () : endif;
<?php
for ($A = 0; $A <= 100; $A++):
$B = (2/3) * ($A - 25);
if($B < 0 ){
echo $B .", Bad <br>";
}else if ($B > 45) {
echo $B .", Good <br>";
}else{
echo $B .", Okay <br>";
}
endfor;
?>
There are some Syntax errors in your code. You have missed ) as well as concatenation operator i.e. .
if ($B < 0 { // if ($B < 0) .... ")" is missing
echo $B ", Bad <br>";} // echo $B .", Bad <br>";} .... "." is missing
here is corrected code :
for ($A = 0; $A <= 100; $A++){
$B = (2/3) * ($A - 25);
if ($B < 0) {
echo $B . " Bad <br>";
}
elseif ($B > 45) {
echo "Good <br>";}
else {
echo "Okay <br>";}
}
<?php
for ($A = 0; $A <= 100; $A++) {
$B = (2 / 3) * ($A - 25);
if ($B < 0) {
echo $B . " Bad <br>";
} else if ($B > 45) {
echo "Good <br>";
} else {
echo "Okay <br>";
}
}
?>
<?php
for ($A = 0; $A <= 100; $A++){
$B = (2/3) * ($A - 25);
if ($B < 0) {
echo $B .", Bad <br>";
}
elseif ($B > 45) {
echo $B ."Good <br>";
}
else {
echo $B ."Okay <br>";
}
}
?>
Try this code.
You had missed the closing paranthesis ) for the if and elseif block and the concatenation operator . in the first echo statement. echo accepts and outputs one string and so you need to combine several strings to make it one and ask echo to output it. endfor; is also not required.
<?php
for ($A = 0; $A <= 100; $A++)
{
$B = (2/3) * ($A - 25);
if ($B < 0)
{
echo $B.", Bad <br>";
}
elseif ($B > 45 )
{
echo "Good <br>";
}
else
{
echo "Okay <br>";
}
}
?>
Using endfor;
for ($A = 0; $A <= 100; $A++):
$B = (2/3) * ($A - 25);
if ($B < 0)
{
echo $B.", Bad <br>";
}
elseif ($B > 45 )
{
echo "Good <br>";
}
else
{
echo "Okay <br>";
}
endfor;
I have replaced the opening { of the for loop with : and the closing brace with endfor;
This is how it works.
I am searching for a php for loop, which should output
22,23,24, 32,33,34, and so on 92,93,94 and all following like 102,103,104,122,123 until 902,903,904,992,993,9949, but not or 12,13,14... 112,13,114 ...912,913,914.
my code:
for ($a = 22; $a <= 1000; $a+=10) {
$b = $a + 1;
$c = $a + 2;
echo "$a <br>";
echo "$b <br>";
echo "$c <br>";
}
I need here an exception, because 112,123 and 124 and so on until 912,913,914 shouldn't be echoed.
All results should be stored in an array.
Try using this code:
$data = array();
for ($a = 2; $a < 10; $a++) {
for ($b = 2; $b <= 4; $b++) {
$c = ($a * 10) + $b;
$data[] = $c;
}
}
I have found all parts do finish kwestgrounds answer with my exceptions.
$delete = array_merge(range (112, 912, 100), range (113, 913, 100),range (114, 914, 100));
$data = array();
for ($a = 2; $a < 100; $a++) {
for ($b = 2; $b <= 4; $b++) {
$c = ($a * 10) + $b;
{
if (in_array($c, $delete)) continue;
}
$data[] = $c;
}
}
Hello all and thanks in advance. I am trying to create a simple script in PHP.
Not getting the actual result of $i variable. Only hello world is printing on browser.
<?php
$d = 9;
$c = 0;
while ($d <9)
{
if ($c==0){
$i="item";
}else {
$i.=$count;
}
echo $i;
$c = $c + 1;
echo $c;
}
echo "hello world";
?>
Not able to figure out, what is missing.
Try to change your while loop like this:
$d = 0;
$c = 0;
while ($d <9)
{
if ($c==0){
$i="item";
}else {
$i.=$count;
}
echo $i;
$c = $c + 1;
echo $c;
$d++;
}
echo "hello world";
<table>
<tr
<td align="center">
<?PHP
$b = 1;
for ($a=1; $a<=1; $a++)
{
echo $b++;
echo "<br>";
}
$b = 2;
for ($a=1; $a<=2; $a++)
{
echo $b++;
}
echo "<br>";
$b = 4;
for ($a=1; $a<=3; $a++)
{
echo $b++;
}
echo "<br>";
$b = 7;
for ($a=1; $a<=5; $a++)
{
echo $b++;
{
echo "<br>";
$b = 12;
for ($a=1; $a<=5; $a++)
{
echo $b++;
}
echo "<br>";
$b = 17;
for ($a=1; $a<=4; $a++)
{
echo $b++;
}
echo "<br>";
$b = 21;
for ($a=1; $a<=3; $a++)
{
echo $b++;
}
echo "<br>";
$b = 24;
for ($a=1; $a<=2; $a++)
{
echo $b++;
}
echo "<br>";
$b = 26;
for ($a=1; $a<=1; $a++)
{
echo $b++;
}
?>
</td>
</tr>
</table>
I am trying to use for loops to make a shape that looks something along the lines of this:
I have managed to do it, but using way too many loops (9), I want to be a little less than this. Anyone have any help?
P.S its meant to be a diamond shape with the numbers 1-26
![sort of like this][1]
How about:
$arr = array(1,2,4,7,12,17,21,24,26,27);
for($i=1; $i<count($arr); $i++) {
for($j=$arr[$i-1]; $j<$arr[$i]; $j++) {
echo $j;
}
echo "<br>";
}
Something like this, just to give an idea:
$i = 0;
$leaps = array(1,2,4,7,12,17,21,24,26);
foreach($leaps as $leap){
for($j = 1; $j<=$i; $j++){
echo $leap++;
}
$i++;
}
Something like this should work:
$rowsUntilMiddle = 5;
$startnumber = 1;
$totalrows = $rowsUntilMiddle*2-1;
$i = $startnumber;
$numInRow = 0;
// each row
for ($row=1; $row<=$totalrows; $row++) {
if ($row <= $rowsUntilMiddle) {
$numInRow++;
} else {
$numInRow--;
}
// each number
for($j=0; $j<$numInRow; $j++) {
echo $i;
$i++;
}
echo '<br />';
}
The following code is displaying INF as the result. How can I fix it?
<?php
function fibonacci($n)
{
$a = 1;
$b = 1;
$result = 0;
for ($i = 0; $i < $n; $i=$i+1)
{
$sum = $a + $b;
$a = $b;
$b = $sum;
if ($a % 2 == 0)
{
$result = $result + $a;
}
}
echo "<br/>" . $result;
}
echo fibonacci(400000);
?>
The number is too big to display, and INF is a pretty good guess :) (fibonacci(1000) gives you a number with 210 digits).
100: 22 digits, 110: 24 digits, 120: 25 digits, 130: 27 digits
If you extrapolate that, you would end up with about (400000 / 10) * 2 = 80000 digits.
The following implements your logic using bcmath to prevent the INF error.
function fibonacci($n)
{
$a = '1'; $b = '1'; $result = '0';
for ($i = 0; $i < $n; $i++) {
$sum = bcadd($a,$b);
$a = $b;
$b = $sum;
if (bcmod($a,'2') == '0') {
$result = bcadd($result,$a);
}
}
echo "<br />".$result;
}
As your fibonacci function doesn't actually return any value, there's no point in echo fibonacci(400000)
EDIT
However, your logic is completely flawed. The following should give you the correct result for the problem you're trying to solve (again using bcmath):
function fibonacci($n)
{
$a = '0'; $b = '1'; $sum = '0';
$sum = '0';
do {
$fib = bcadd($a,$b);
$a = $b;
$b = $fib;
if (bccomp($fib,$n) == -1) {
if (bcmod($fib,'2') == '0') {
$sum = bcadd($sum,$fib);
}
}
++$i;
} while (bccomp($fib,$n) == -1);
return $sum;
}
echo fibonacci(4000000);
Rather than simply executing it to get the result, look to see how it works and what it's actually doing