Link to home
Start Free TrialLog in
Avatar of prinLeA
prinLeA

asked on

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
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

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
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.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Some good "getting started" resources are available here:
https://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.
https://www.experts-exchange.com/articles/12310/PHP-Variables-and-References.html
Avatar of prinLeA
prinLeA

ASKER

Thanks everyone for the help, but I have to give the points to this expert because he explained it thoroughly.
You are welcome