I have moved this thread to the General Discussion forum. PHP Code forum is for questions or discussions of PHP code.
genix2011 has given you a start on understanding, but let me add a few points that may help you further:
A
web server is the software that SENDS web pages to a browser. Every time you use a web browser (IE, Firefox, Safari, Chrome, Opera) to visit a web page, your browser sends a REQUEST to the web server that you address or link to (
http://www.google.com, whitehouse.gov, mit.edu, facebook.com.....). If that address can be found, and if a particular document you are requesting can be found there, the
web server is the software that sends that document back so your browser can display it. Probably 99% of the web servers in the world are either Microsoft's Internet Information Server (IIS) or Apache, mostly dependent on whether the site's servers are running under Windows or Linux. Without a web server, you can't view a web page that has any PHP code in it, because PHP is a
server-side language, interpreted in the web server
before it sends the page to the browser. A browser can display a plain
HTML file without a web server, but not if there is any PHP code involved. By contrast, Javascript is a
client-side language, interpreted in the
browser. As you know, HTML is the markup language that instructs a browser how to display a web page.
Thus, when developing web pages with PHP, it's normal practice to have a web server installed on your "development" computer (at home, in your case) so you can test new ideas without exposing all your experimentation to the whole world by developing on your "production" server. It's also easier and faster, since you don't have to constantly use FTP to upload new versions to the production server. Fortunately, Apache is fairly easy to install on either a Windows or a Linux computer, so that's what most developers do. PHP parsing and execution is built into Apache (and can be added to IIS), so all you need to do is download the PHP libraries and configure Apache so it knows where to look for them.
MySQL is probably the most popular serious database in the world. It is used by the largest companies and organizations for mission-critical applications that require top performance and reliability under heavy use by large numbers of users. MySQL is also relatively easy to install on even a modest home computer and is supported by PHP so you can connect to it, send queries, and receive recordsets from it.
So those are the 3 elements of setting up a local "development system" on your own computer so you can write PHP code in web script files, test them on your local machine, then when everything is working properly, FTP them to the "production" server.
The standard "address" of a web server (Apache or IIS) that is installed on the same machine as the browser that's requesting a web page from it is
localhost. So when you have installed, say Apache, on your desktop or laptop, and you open a browser on the same computer, instead of entering something like "
http://youtube.com", you enter "
http://localhost/index.php" or just "localhost" or "localhost/xyz.html".
Be aware that there ARE a few differences, because the environment (available software, directory structure, etc.) is likely to be at least a little different between your "development" and "production" systems. So don't be shocked if, occasionally, something works one place and not the other. As an example, you probably won't have an SMTP (email) server installed at home, so you may not be able to test a script that sends email.
Yes, there are "clean" solutions to most of the differences, such as your MySQL connections. Here are a few examples:
PHP maintains a lot of "server environment variables", such as the name and path of the currently running script. These special global variables all begin with
$_ followed by all-uppercase names. See
http://php.net/manual/en/reserved.variables.php and especially, click on
$_SERVER.
By using PHP's
include() or
require() functions to establish your MySQL connection, and in the include file, testing to see if the global variable
$_SERVER['SERVER_NAME'] is
localhost, as shown below, your script can supply the appropriate host name, user name, and password for the MySQL database, depending on whether the server is or is not
localhost.
Code: Select all
<?php
if($_SERVER['SERVER_NAME'] == 'localhost' ) {
$env = "devel";
} else {
$env = "prod";
}
if($env == "devel") {
$host = "localhost";
$user = "root";
$pwd = "";
} else {
$host = "fairbanks.db.1077304.hostedresource.com";
$user = "wurlitzer";
$pwd = "XYZ123";
}
mysql_connect($host, $user, $pwd);
mysql_select_db("aardvark");
?>
(by the way, I have changed the information above, it's not real)
With an include file like the above (with proper values, of course), each PHP script that needs to connect to the MySQL database would have a line like this:
and that would work correctly whether you ran it locally or on the production server.
That doesn't eliminate all the issues, because your production database may contain different data than your development database, so there are many other situations that you will need to consider, but that should give you some idea of the process.