Confused about relationship between HTML and PHP for forms.

Q: why is this php file, form.php not returning anything in the browser?

PHP files are not like flat files like HTML, JPG, CSS, JS, and others. a PHP file is an actual program that is like a hybrid between C and HTML.

When you "open" a php file like "www.fakewebsite123.com/page.php" you are running the page.php program. The web server only displays the "printf" like output OR whatever is in "naked HTML Mode." It's a bit confusing, and coming from the C programming language makes it ultra confusing. But, the best way to think of it is that PHP is an injection of C language into HTML on the Server Side.

Q: And also, how come when with a php file in one's OS explorer/finder, you're not able to Open With any browser?

PHP is actually an interpreted language. The PHP Script must run through a PHP interpreter. In Linux and on the CS50 appliance this interpreter is called PHP-CGI. In windows, it's php.exe. PHP is an external 3rd party program that has to be installed. (The CS50 guys set it up for you in the appliance)

Part 1) When you open a PHP file on your computer, you will see the source code. Much like your C programs. (Ex: cipher.c)

Part 2) When you "make" cipher.c you get the "cipher" executable.

Part 3) When you run cipher at the terminal (using: ./cipher) you are executing (running) the program.

For PHP, parts 2 and 3 are replaced with this: At the terminal, type "PHP-CGI page.php" . This will run your program. Notice that I didn't have to compile (part 2). I simply just pointed to the source code and it ran it. Awesome advantage at not having to compile. The disadvantage is that errors are dumped right onto the webpage in a crappy way.

Recommendation:

Make a simple php file named "test.php"

Put this code in it: (after that, open it through the web page (http://localhost/page.php))

<!DOCTYPE html>
<html>
<body>

<h1>This is a header line. This is interpreted like HTML</h1>

<?php
// Anything between "?php" and "?" is interpretted like C code. Notice that your web output is like a C program, not like flat HTML. 
for ($x = 0; $x <= 10; $x++)
   echo "$x ";

?>   

<h1>This is a footer line. This is interpreted like HTML. Notice that this is after your PHP program. The PHP program was "injected into the web page.</h1>

</body>
</html>
/r/cs50 Thread