How does PHP work with HTML files?

In summary,For example, you can use index.php instead of index.html if your server is configured to find it by default. However, you may need to configure your webserver to find it.
  • #1
kolleamm
477
44
For example instead of uploading an index.html to a server, would I just use index.php instead?
Basically I want to be able to use PHP code to make my page more interactive.
 
Technology news on Phys.org
  • #2
Yes but you may need to configure your webserver to find that by default, especially if you have both index.html and index.php
 
  • #3
Greg Bernhardt said:
Yes but you may need to configure your webserver to find that by default, especially if you have both index.html and index.php
Would it work if I only had a index.php?
 
  • #4
kolleamm said:
Would it work if I only had a index.php?
Are you using apache webserver?
 
  • #5
kolleamm said:
Would it work if I only had a index.php?
As he said, if it's in the list of defaults, yes, otherwise no.

Look for a .htaccess file.BTW, stackoverflow.com is to programming what PF is to science.
Register there. Ask questions. You won't regret it.
 
  • #7
kolleamm said:
For example instead of uploading an index.html to a server, would I just use index.php instead?
Basically I want to be able to use PHP code to make my page more interactive.

If your server is connected with a PHP engine (as is the case for WAMP you installed), then yes. When a request is made to the web server, the server "recognizes" that it is not HTML (from the file extension) and the page is "sent" to the PHP interpreter and this in turn returns just HTML, that will be sent back to client. This happens in very rough lines - you can read the details if you want, by googling it. So, you can generate HTML inside your index.php file in the usual manner
PHP:
echo "<HTML tag></HTML tag>
            ...
           ";

along with your other PHP code

or just use the closing PHP tag
Code:
?>
, write pure HTML and open again PHP in order to write other PHP code you need and so on - effectively creating some interspersed HTML inside your PHP code.
 
  • #8
Wait, you are seriously misunderstanding how websites work. PHP will not make your page interactive, Javascript will.

When you get a web request, Apache loads whatever you point it at. It can be a static HTML page, which it will simply return, or it can be an executable which it will run and return the output, or it can be PHP. The PHP is run just like an executable, exactly the same as if you had put
Code:
php index.php
In your command line. (Not exactly, but for these purposes, it's good enough.)

Like if you told apache to run an executable, it listens to stdout, and then returns that. echo sends data to stdout.
 
  • #9
newjerseyrunner said:
The PHP is run just like an executable
...on the server. The viewer's browser sees only the HTML/CSS/Javascript code that the PHP code outputs. I don't think there is any way for the browser to distinguish whether the code that it receives comes from a PHP script or not, assuming of course that the code is properly written so it doesn't simply send the browser the raw PHP code. (I'll happily stand corrected if that's not the case!)
 
  • Like
Likes QuantumQuest
  • #10
I'm pretty sure I'm misunderstanding how PHP works.
As far as I know - A PHP file generates an HTML page. Then I wonder how do you generate another HTML file. Also wouldn't using JS be a problem since it makes your code public and others can copy it?
 
  • #11
kolleamm said:
I'm pretty sure I'm misunderstanding how PHP works.
As far as I know - A PHP file generates an HTML page. Then I wonder how do you generate another HTML file.

PHP is a server-side scripting language. This means that you can do various computations and calculations i.e. programming with it, connect to a database and store/retrieve information and do protocol operations. You also output HTML code, in the manner that I described in #7. You also include whatever other client code you have (like CSS and JavaScript) and then it is the job of client program engines to interpret all these(HTML, CSS, JavaScript). One very important thing is to distinguish in a clear way, what takes place on the server side and what on the client side (your browser or whatever client software "talks" to the server).

kolleamm said:
Also wouldn't using JS be a problem since it makes your code public and others can copy it?

If you write JavaScript code and send it as it is to the client, then yes it can be copied/stolen. That's were the obfuscation concept comes into play. Obfuscation is basically converting code to a form that is unreadable, so there's no point to be stolen. Take a look at this tool for instance. There are many such free tools on the net.
 
  • #12
QuantumQuest said:
If you write JavaScript code and send it as it is to the client, then yes it can be copied/stolen. That's were the obfuscation concept comes into play. Obfuscation is basically converting code to a form that is unreadable, so there's no point to be stolen. Take a look at this tool for instance. There are many such free tools on the net.
You can do this - but why?

There is (should be) nothing in your JavaScript or HTML that is sensitive. Code, like oxygen, should be free too everyone. Data is the currency.
 
  • #13
DaveC426913 said:
You can do this - but why?

There is (should be) nothing in your JavaScript or HTML that is sensitive. Code, like oxygen, should be free too everyone. Data is the currency.

Yes I agree. As a web developer I know that very well. I just gave the OP the way that he/she can do this should this be needed - there are many cases that it does but in general it is not something mandatory. It depends on a number of factors.
 
  • #14
QuantumQuest said:
Yes I agree. As a web developer I know that very well.
It was directed at the OP, who said:
wouldn't using JS be a problem since it makes your code public and others can copy it?
If one is worrying about others copying one's HTML and JS, one is looking at web dev wrong.
By its very nature, web code is served to the user.
 
  • #15
DaveC426913 said:
If one is worrying about others copying one's HTML and JS, one is looking at web dev wrong.
By its very nature, web code is served to the user.

While I don't disagree in general, this has finally become an ideal - unfortunately like many other things, the primary reason being that there is a whole new "generation" out there of copy-pasters or "the bad guys" in this case if you will, and as JavaScript coding is already on steroids for a fairly long time, there is real need to protect your code from prying eyes. The average user may not care about this but development world vastly does. It is the case that everyone can find virtually everything, regarding resources to learn and above all for free, but not everyone can create code diamonds and more importantly some people are not willing to take the pains to write their own code at all. Now, if you combine this with having a job in a (mainly big) development company, code becomes a real asset and there is no other way to protect it, than obfuscating it. The same rationale is applied to other programming languages - beyond JavaScript, as well. Now, if you ask me if this is morally correct, it is not in my opinion but clearly, some unmoral behaviors created this need. In some companies, obfuscation is a formal part of their policies and there's no way to bypass it. So, beyond data that is the real asset as you pointed out, code is an asset as well in many cases and needs protection.

On the other extreme, code obfuscation is used some times to serve everything but innocent purposes, but this is an inevitable path that utilization of a thing creates beyond its primary purpose.
 
  • #16
QuantumQuest said:
While I don't disagree in general, this has finally become an ideal - unfortunately like many other things, the primary reason being that there is a whole new "generation" out there of copy-pasters or "the bad guys" in this case if you will, and as JavaScript coding is already on steroids for a fairly long time, there is real need to protect your code from prying eyes.
Beyond the first decade of web dev, when we had a 20th century idea of proprietary software, I cannot recall the last time I heard of a web dev or a web dev shop think about their front-end dev as needing protection. It's now about deliberately making one's work as easy as possible to grab - any dev or dev shop worth their salt puts their work in GitHub or other repos with the full intention of people using it.

Theft is just not how the front end world is. And fear of theft is .. archaic - backwards.
 
  • Like
Likes phinds
  • #17
DaveC426913 said:
Theft is just not how the front end world is. And fear of theft is .. archaic - backwards.

The only people worrying about obfuscating JS code are those that are doing fishy things.
Like those scam 'whose viewing your facebook profile' websites.
 
  • Like
Likes DaveC426913
  • #18
kolleamm said:
For example instead of uploading an index.html to a server, would I just use index.php instead?
Basically I want to be able to use PHP code to make my page more interactive.
yeah you can do easily set your server and in the directory placed the index.php and it's start working !
 
  • #19
DaveC426913 said:
Beyond the first decade of web dev, when we had a 20th century idea of proprietary software, I cannot recall the last time I heard of a web dev or a web dev shop think about their front-end dev as needing protection. It's now about deliberately making one's work as easy as possible to grab - any dev or dev shop worth their salt puts their work in GitHub or other repos with the full intention of people using it.

Now, to clarify things a bit, it is not like that. What do you mean by web dev and web dev shops? It is about open source code or not. Open source code is put as we all know on GitHub and that's great, but up to there. Proprietary software is still out there and is still developed. A company may have only open source code or may have proprietary code and open source code - take Oracle as an example, there is a real lot of others as well. Front end development is no different at all. Haven't you seen big sites with obfuscated JavaScript code here and there? If not then check it.

JorisL said:
The only people worrying about obfuscating JS code are those that are doing fishy things.
Like those scam 'whose viewing your facebook profile' websites.

That's just a bold statement. As I said in #15

QuantumQuest said:
On the other extreme, code obfuscation is used some times to serve everything but innocent purposes, but this is an inevitable path that utilization of a thing creates beyond its primary purpose.
 
  • #20
The problem with obfuscation is that it's either easy to undo or it will increase the amount of code that's executed, a lot of which is pure overhead.
What most big sites use is minified code =/= obfuscated. Even in the development tools the code gets reformatted in that case.
 
  • Like
Likes DaveC426913
  • #21
QuantumQuest said:
On the other extreme, code obfuscation is used some times to serve everything but innocent purposes, but this is an inevitable path that utilization of a thing creates beyond its primary purpose.
I disagree that it is inevitable. I think we've dodged that bullet.
 
  • #22
JorisL said:
The problem with obfuscation is that it's either easy to undo or it will increase the amount of code that's executed, a lot of which is pure overhead.

That's true but there is no other way to protect your code - if you need to, than doing a complex obfuscation. Of course as browsers have to be able to translate the JS obfuscated code to its original form then any individual can do it, but with some fair efforts. Talking - beyond JavaScript, for code in general, there is very good reason in many cases for (legitimate) use of obfuscation.

JorisL said:
What most big sites use is minified code =/= obfuscated. Even in the development tools the code gets reformatted in that case.

It is also true that JS code minification is used, the goal being improving performance (download times and bandwidth usage) but this has nothing to do with obfuscation.
 
  • #23
DaveC426913 said:
I disagree that it is inevitable. I think we've dodged that bullet.

I say this in the sense that everything can potentially be good or bad depending on its usage. But yes, there have been some very good efforts and results in defeating obfuscated malware.
 
  • #24
kolleamm said:
I'm pretty sure I'm misunderstanding how PHP works.
As far as I know - A PHP file generates an HTML page. Then I wonder how do you generate another HTML file.

when someone enter « http://example.com/myfile.html », the server at «example.com» will take the text file «myfile.html» on its hard disk and send it to the user. It is a simple text file, which is written with a markup language that the client's browser can interpret. So when the browser reads:

HTML:
<span style="color:red;">This text is red.</span>

It actually shows the user: This text is red.

But if you read the file with notepad (which is a text editor), it will simply show you the html code:

<span style="color:red;">This text is red.</span>

PHP is a program that can read text files too. If the server (ex.: Apache) has access to this program, it can be set up such that if the file requested by the user ends with a .php extension (ex.: « http://example.com/myfile.php »), the server will send «myfile.php» to PHP, which will analyze it before outputting an HTML text. The server will send that HTML text to the user.

A PHP file is basically an HTML file with an extra element: <?php [...] ?> . So the PHP program reads the PHP file just like a regular HTML file, but when it encounters that element, it knows that it contains special instructions that it must perform before sending the HTML. The result of those instructions could have no effect on the outputted HTML (ex.: It could record the IP address of the user who requested the page and store it somewhere on the server) or it could create some HTML markup to insert within the text file (replacing the <?php [...] ?> that produced it).

Nowadays, PHP files often contains no HTML at all, it just begins with "<?php" and everything that follows are PHP instructions, which, of course, must produce some HTML to send back to the user.

For example a PHP file could read one of these two ways which would spit out the same thing as «myfile.html»:

PHP:
<?php $color = "red"; ?>
<span style="color:<?php echo $color; ?>;">This text is red.</span>
On the first line, you have PHP code that stores the text "red" into the variable $color. On the 2nd line, you have HTML markup with some PHP between «<span style="color:» and «;">This text is red.</span>» which basically says "write the value stored in $color".

PHP:
<?php

function setColor($color){
    return 'color:'. $color . ';';
}

function writeText($text, $element, $attribute){
    echo '<' . $element . ' style="' . $attribute . '">' . $text . '</' . $element . '>';
}

writeText('This text is red.', 'span', setColor('red'));

?>

Here there is no HTML at all. You have the function «setColor» defined that returns a string. You also have the function «writeText» defined which writes the desired HTML element.

Finally, the «writeText» function is executed with the desired input.

Note that a PHP file could have only pure HTML with no PHP element at all. In that case, the PHP program would output exactly what was inputted. So «myfile.php» could also simply be:

PHP:
<span style="color:red;">This text is red.</span>
 
Last edited by a moderator:

1. Does PHP completely replace HTML files?

No, PHP and HTML serve different purposes and work together to create dynamic web pages. HTML is used for creating the structure and content of a webpage, while PHP is used for server-side scripting and can add functionality to an HTML file.

2. Can I use only PHP without HTML?

No, PHP is a server-side scripting language and cannot be rendered on its own. It needs to be embedded in an HTML file to be displayed in a browser.

3. Is PHP better than HTML?

It is not fair to compare PHP and HTML as they have different functions. PHP is used for dynamic content and server-side scripting, while HTML is used for creating the structure and content of a webpage. Both are important for creating a functioning website.

4. Can I convert an HTML file to PHP?

Yes, you can convert an HTML file to PHP by changing the file extension from .html to .php. However, the file will only be converted if there is PHP code embedded in the file, otherwise, it will still function as an HTML file.

5. Do I need to learn PHP if I know HTML?

It is not necessary to learn PHP if you only plan on creating basic static websites. However, if you want to create dynamic websites with advanced functionality, then learning PHP can greatly enhance your web development skills.

Similar threads

  • Programming and Computer Science
6
Replies
187
Views
8K
  • Programming and Computer Science
Replies
5
Views
370
  • Programming and Computer Science
Replies
21
Views
5K
  • Programming and Computer Science
Replies
6
Views
1K
Replies
3
Views
2K
  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
20
Views
522
  • Programming and Computer Science
Replies
13
Views
2K
Back
Top