/    Sign up×
Community /Pin to ProfileBookmark

declaring and printing a matrix, debugging

Hello. Here is my code:

[code]
<!DOCTYPE html>
<html>
<head>
<title>hey I am a title</title>
</head>
<body>

<?php

$matrix=array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);

function writeMatrix(){
for($i=0;$i<=2;$i++){
echo “xA”;
for($j=0;$j<=2;$j++){
echo $matrix[$i][$j].” “;
}
}
}

writeMatrix();

//echo $matrix[1][1];

?>

</body>
</html>
[/code]

Could you please help me to find a mistake? The code doesn’t output anything. Thank you.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 13.2021 — Firstly, you need to pass your $matrix into the function, otherwise it has no idea what you're referring to. (Inside a function definition you have a separate variable scope.)

Secondly, you can leverage foreach() so that the function doesn't need to know how many rows/columns there are.

Thirdly, might as well output it as a table, since it's tabular data. :)
[code=php]
<!DOCTYPE html>
<html>
<head>
<title>hey I am a title</title>
</head>
<body>
<?php
$matrix=array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);

function writeMatrix($matrix) // add function parameter
{
foreach ($matrix as $row) {
echo "<tr>";
foreach ($row as $element) {
echo "<td>$element</td>";
}
echo "</tr>n";
}
}

echo "<table>n";
writeMatrix($matrix); // call function with new parameter
echo "</table>";
?>
</body>
</html>
[/code]

Oh, fourthly, assuming you're using a version of PHP that's not horribly out of date, you can use a shorter array notation:
[code=php]
$matrix=[
[1,2,3],
[4,5,6],
[7,8,9]
];
[/code]
Copy linkTweet thisAlerts:
@codewitchauthorNov 13.2021 — Thank you NogDog. You are the best!

I would add the beginning and the end of the table tag to the function body:

<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;hey I am a title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php

$matrix=array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);


function writeMatrix($matrix)
{
echo "&lt;table&gt;n";
foreach ($matrix as $row) {
echo "&lt;tr&gt;";
foreach ($row as $element) {
echo "&lt;td&gt;$element&lt;/td&gt;";
}
echo "&lt;/tr&gt;n";
}
echo "&lt;/table&gt;";
}

writeMatrix($matrix);

?&gt;

&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @codewitch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.11,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...