PHP Getting Started

Getting Started with PHP

Here, you will see how simple it is to create dynamic web pages using PHP. Before you begin, ensure you have a code editor and some basic knowledge of HTML and CSS.

If you're new to web development, start learning from here »

Alright, let's dive right in.

Setting Up a Local Web Server

PHP scripts run on a web server with PHP. So before writing any PHP program, you need to have the following software installed on your computer.

  • The Apache Web Server
  • The PHP engine
  • The MySQL database server

You can either install them separately or use a pre-configured package for your operating system like Linux or Windows. Popular pre-configured packages are XAMPP and WampServer.

WampServer is a web development environment for Windows. It allows you to create web applications with Apache2, PHP, and a MySQL database. It also includes the MySQL administrative tool PhpMyAdmin to easily manage your databases via a web browser.

The official website for downloading and installation instructions for WampServer: http://www.wampserver.com/en/


Creating Your First PHP Script

Now that you have successfully installed WampServer on your computer, we will create a very simple PHP script that displays the text "Hello, world!" in the browser window.

First, click on the WampServer icon on your Windows taskbar and select the "www directory". Alternatively, you can access the "www" directory by navigating to C:\wamp\www. Create a subdirectory in the "www" directory, let's call it "project".

Next, open your preferred code editor and create a new PHP file. Then type the following code:

<?php
// Display greeting message
echo "Hello, world!";
?>

Now save this file as "hello.php" in your project folder (located at C:\wamp\www\project), and view the result in your browser by visiting this URL: http://localhost/project/hello.php.

Alternatively, you can access the "hello.php" file by selecting the localhost option and then choosing the project folder from the WampServer menu on the taskbar.

PHP can be embedded within a standard HTML web page. This means you can write PHP statements inside your HTML document, as shown in the following example:

<!DOCTYPE HTML>
<html>
<head>
<title>PHP Application</title>
</head>
<body>
<?php
// Display greeting message
echo 'Hello World!';
?>
</body>
</html>

You will understand what each of these statements means in the upcoming chapters.