PHP

May 20, 2023

PHP is a server-side scripting language designed for web development. It is used mainly for creating dynamic web pages and interactive web applications. Its name originally stood for “Personal Home Page”, but it has since been rebranded as “Hypertext Preprocessor”. PHP is an open-source language and is maintained by a community of developers.

PHP is designed to be embedded into HTML code and can be used with a variety of web servers, including Apache, Nginx, and IIS. PHP code is executed on the server, and the resulting HTML is sent to the client browser. This allows for dynamic content to be generated based on user input or database queries.

History of PHP

PHP was created in 1994 by Rasmus Lerdorf, a Danish-Canadian programmer. Lerdorf created PHP as a set of Common Gateway Interface (CGI) scripts to track visits to his personal website. He later released the scripts as open source software.

In 1997, two developers named Andi Gutmans and Zeev Suraski rewrote the PHP parser from scratch, creating the basis for what is now known as PHP 3. This version of PHP included support for ODBC databases and improved performance over the original version.

PHP 4 was released in 2000 and included support for object-oriented programming, and improved support for web development. In 2004, PHP 5 was released, which included significant improvements in performance, security, and language features.

PHP 7 was released in 2015 and included significant performance improvements and new language features, including support for scalar type declarations and anonymous classes.

Features of PHP

Server-side scripting

PHP is typically used for server-side scripting, which means that PHP code is executed on the server before the resulting HTML is sent to the client browser. This allows for dynamic content to be generated based on user input or database queries.

Embedding in HTML

PHP code can be embedded directly into HTML code, allowing for dynamic content to be generated on the fly. PHP code is delimited by <?php and ?> tags, which allows for easy integration with HTML code.

Database integration

PHP includes support for a wide range of databases, including MySQL, Oracle, and PostgreSQL. This allows for easy integration with database-driven web applications.

Object-oriented programming

PHP includes support for object-oriented programming, which allows for the creation of reusable code and the development of more complex applications.

Cross-platform compatibility

PHP is a cross-platform language, which means that PHP code can be run on a variety of operating systems, including Windows, macOS, and Linux.

Open source

PHP is an open-source language, which means that the source code is freely available for use, modification, and distribution.

Usage of PHP

PHP is primarily used for web development, including the creation of dynamic web pages and web applications. PHP code can be embedded directly into HTML code, which allows for dynamic content to be generated on the fly.

PHP is often used in conjunction with a web server, such as Apache or Nginx. When a user requests a web page, the web server sends the request to PHP, which executes the PHP code and generates the resulting HTML. This HTML is then sent back to the web server and sent to the user’s browser.

PHP can also be used for command-line scripting and desktop application development. PHP includes support for a variety of operating systems, which makes it a popular choice for cross-platform development.

Examples of PHP Code

Hello World

The following PHP code will output the string “Hello World” to the browser:

<?php
echo "Hello World";
?>

Database Query

The following PHP code will connect to a MySQL database and retrieve a list of users:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// SQL query
$sql = "SELECT * FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // Output data of each row
  while($row = $result->fetch_assoc()) {
    echo "Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
  }
} else {
  echo "0 results";
}

$conn->close();
?>

Object-Oriented Programming

The following PHP code defines a class called “Person” with two properties, “name” and “age”, and a method called “greet”:

<?php
class Person {
  public $name;
  public $age;

  public function greet() {
    echo "Hello, my name is " . $this->name . " and I am " . $this->age . " years old.";
  }
}

$person = new Person();
$person->name = "John";
$person->age = 30;
$person->greet();
?>