PHP super basic question

I got the codes below from W3Schools there is no problem with the codes. I would just like to know how does PHP and the WAMP server understand each other with just variables. I'm trying to learn PHP, I'm using Windows and Wamp server. I will highlight below what I don't understand.

<!DOCTYPE html>
<html>
<body>

<?php
$cars = array
   (
   array("Volvo",22,18),
   array("BMW",15,13),
   array("Saab",5,2),
   array("Land Rover",17,15)
   );
     
for ($row = 0; $row <  4; $row++) {  
/*
HOW DID THE SERVER UNDERSTAND PHP THAT THE ROW VARIABLE WILL OUTPUT THE NUMBER OF ROWS? I HAVE CHANGE THIS IN MY LOCAL MACHINE WITH ANOTHER VARIABLE AND IT CAN STILL WORK OF COURSE I HAVE TO CHANGE ALL THE ROW VARIABLE BELOW. PHP TUTORIAL TEACH YOU HOW TO WRITE PHP BUT IT DOES NOT TEACH YOU HOW DOES THE INTERPRETER OR THE SERVER UNDERSTAND PHP LANGUAGE HOW DOES IT WORK?
*/
   echo "<p><b>Row number $row</b></p>";  
/*
CORRECT ME IF I'M WRONG THE SERVER WOULD JUST SEE THAT THERE IS A VARIABLE, THAT IT NEEDS TO OUTPUT SOMETHING THAT IS LESS THAN FOUR AND IT HAS TO INCREMENT IT UNTIL FOUR.
*/
   echo "<ul>";
   for ($col = 0; $col <  3; $col++) {
     echo "<li>".$cars[$row][$col]."</li>";
/*
WHY IS THE ROW VARIABLE USED ABOVE HERE AGAIN? IS IT TO DEFINE IT SO THAT THE SERVER/INTERPRETER OF PHP LANGUAGE WOULD UNDERSTAND WHAT ACTUALLY IT IS? WHY IS THE COL VARIABLE "$COL OUTPUT THE LAST VALUE? AND NOT THE SECOND COLUMN? IT UNDERSTOOD THAT THE COL VARIABLE MUST OUTPUT THE LAST COLUMN VALUE AND THE ROW VARIABLE MUST OUTPUT THE VALUE IN THE MIDDLE COLUMN?
   }
   echo "</ul>";
}
?>

</body>
</html>

Down below is the output:

Row number 0

Volvo
22
18
Row number 1

BMW
15
13
Row number 2

Saab
5
2
Row number 3

Land Rover
17
15

In summery can someone please explain how does the server or the interpreter of the PHP language understand it? If I learn how it can think - how it process information I would learn how to do some PHP programming. Most tutorials on the internet just teach how to write PHP, the syntax and sample codes but as a programmer I must learn how the interpreter process the PHP language. They say PHP is a lose language unlike other language you need to declare the variables, and that confuses me, how would the server understand what it will output.

Kind Regards,

PrinLea
prinLeAAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
As far as the interpreter is concerned, it does not matter if the variable is called $row, $i, $somethingverylongandhardtoread or $somethingelse.

The names are just so the humans that read the code can understand it better.

You first give a value to a variable and then PHP will replace the variable with that value, wherever it finds it.

HTH,
Dan
Dave BaldwinFixer of ProblemsCommented:
The PHP site http://php.net/ has probably the best documentation of any programming language.  This http://php.net/manual/en/control-structures.for.php is the page for the 'for' loop and it explains each part of the command.  While W3Schools is good, http://php.net/ is much more complete and is written by the people who write the PHP interpreter.
Julian HansenCommented:
Some basic background on how PHP works.

When you make a request to the webserver the latter has rules setup that help it understand what to do with the request. In the case of PHP the server has a rule that says any files with .php extension must be passed to the PHP interpreter for processing.

When this happens the PHP module takes over processing of the request - it reads the requested file and parses it. This means it uses rules that define the PHP language (called a grammar) and a parser that uses the rules to validate the code and to pass the validated instructions to an interpreter that "executes" the code.

Once this process happens the rest is very much like any other computing language - the constructs of the language determine the flow and output of the application. In the case of PHP when the server passes control to the PHP interpreter it provides the process with a means to send output back to the browser. All of PHP's output statements are then sent out through this connection.
When the PHP process terminates this signals to the HTTPD process that the script has finished processing and the connection is closed.

CORRECT ME IF I'M WRONG THE SERVER WOULD JUST SEE THAT THERE IS A VARIABLE, THAT IT NEEDS TO OUTPUT SOMETHING THAT IS LESS THAN FOUR AND IT HAS TO INCREMENT IT UNTIL FOUR
No, the server does not see the variable, PHP handles the processing of the code and all that comes back is the resulting output (string) produced by the script - the server has no view on the internal function of how that string / output was created.

WHY IS THE ROW VARIABLE USED ABOVE HERE AGAIN? IS IT TO DEFINE IT SO THAT THE SERVER/INTERPRETER OF PHP LANGUAGE WOULD UNDERSTAND WHAT ACTUALLY IT IS?
No, the code is performing a loop through the array of cars and listing their values as rows i.e. for the first car there would be three rows that list the make followed by the second two numeric values
<p>Row number 0</p>
<ul>
  <li>Volvo</li>
  <li>22</li>
  <li>18</li>
</ul>
<p>Row number 1</p>
<ul>
  <li>BMW</li>
  <li>15</li>
  <li>13</li>
</ul>
...

Open in new window

The reason the "row variable is being used again" is so that the code can output each of the values associated with each vehicle. Row 0 refers to the first item in the array - which has three values - so a second loop is created to output each of those. Then Row is incremented by 1 to access the second item of the array - which has 3 values so a loop is run to output those etc.
WHY IS THE COL VARIABLE "$COL OUTPUT THE LAST VALUE?
Strictly speaking $col is not output - it is used as an index into the array. The data is made up of an array of arrays. If you were to display this data in spreadsheet (for example) there would be rows of 3 values which would be represented in columns. When accessing arrays within arrays - in this case we call the $cars structure a 2 dimensional array - it has an array inside each array element - to access the individual data cells we need to tell the PHP interpreter which of the first array items we want to access (the row) followed by which item in that array we want to access.

If we said this
echo $cars[0];
We would get the following output
Array()
That is because the PHP interpreter will look at the first item in the $cars array and see that it is an array. It does not know how to interpret the data in that array (because you have not told it) and so it just reports that the item is  an array.
To output a value you have to do this
echo $cars[0][0]
Which tells PHP I want you to find the first item (index 0) in the $cars array and I then want you to take the first item of that array and output it. Now the output will be
Volvo

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Ray PaseurCommented:
Some good "getting started" resources are available here:
http://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

PHP has both variables and references.  The differences are shown in this article.
http://www.experts-exchange.com/articles/12310/PHP-Variables-and-References.html
prinLeAAuthor Commented:
Thanks everyone for the help, but I have to give the points to this expert because he explained it thoroughly.
Julian HansenCommented:
You are welcome
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.