Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

make cartesian coordinate with for-loop in php

i’m trying to make cartesian coordinate for my coursework and can only get here, I’m grateful if anyone can help me to fix this "0" line

<?php
$n=10;
for($i=-10;$i<=$n;$i++){
    for($j=-10;$j<=$n;$j++){
        if($i==0 || $j==0){
            echo " $i ";
        }   else {
                echo "   ";
            }
        
    }
    echo "\n";
}

Current output
What i expected

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can try this in mode console.
But it’s not really readable

But the principle is there

$n=10;

for($y=$n;$y>-$n;$y--)
{
    for($x=-$n;$x<=$n;$x++)
    {
        $infoDisplay = "  ";

        if($x==0)   $infoDisplay = $y;
        if($y==0)   $infoDisplay = $x;

        echo $infoDisplay;
    }

    echo "\n";
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading