Skip to content

PHP - Getting Started

PHP was invented by Rasmus Lerdorf and its first release was in 1995. Lerdorf wanted a server-side scripting language where he could process logic and output HTML and other useful formats. On release, PHP stood for Personal Home Page, but the definition has since expanded to the recursive backronym PHP: HyperText Preprocessor. It is now a large part of the web, powering a variety of sites from WordPress blogs to Facebook.

While sandbox environments are great, one of the best ways to learn is to jump in and start making something.

Follow the official guide from the PHP group for your OS.

Here are two files from w3schools that demonstrate creating a form and handling it server-side.

├── index.php
└── welcome.php
index.html
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>

Submitting the form on index.php directs the client to welcome.php, where the body of the POST request is accessed from the $_POST variable.

You can serve the application on any non-restricted port (1024-49151). Here’s an example of serving the application on port 8000.

Terminal window
# from `man php`
# --server addr:port
# -S addr:port Start built-in web server on the given local address and port
# --docroot docroot
# -t docroot Specify the document root to be used by the built-in web server
# The `.` after the `-t` flag specifies the current directory as the one to be served.
php -S localhost:8000 -t .

View the application in the browser on http://localhost:8000 and try submitting the form. Play with the code to see what happens.