Programming

PHP

What is PHP?

PHP usually runs on the server, behind Nginx or Apache. The browser receives finished HTML, and sometimes JSON. The language has been for the web since 1995. Modern PHP has classes, types, and the Composer package manager.

What is PHP used for?

PHP is used for a site, an admin panel, and a simple API. Many hosts already provide it.

  • A WordPress site
  • An app with Laravel
  • Data from a form
  • A page with MySQL

What can you do with PHP?

With PHP you write an article site, a cabinet with login, and a page after a payment. A template can mix into HTML, but a framework such as Laravel separates that. A command-line script is possible, but the main place is the web.

Is PHP hard to learn?

Old examples are messy, so the language looks hard. Modern PHP with types and Composer is straighter. The hard part is security: do not paste user text straight into SQL or HTML.

Advantages and limits

Advantage: it is ready on a host, the web libraries are many, and the WordPress world is large. Limit: it is not chosen for a long-lived desktop program or a mobile app. Old code and new code can stay mixed in one project.

Basic ideas

A PHP file opens with <?php. A variable starts with $. A web request arrives in the $_GET and $_POST arrays.

  • Composer installs packages
  • PDO is for a safer database query
  • Laravel gives routes and templates
  • php -S opens a local server

A short example

This file prints one line to the browser or the terminal. The command is php hello.php.

<?php
echo "Hello";

Common questions

  • Is PHP dead? No. WordPress and many sites still run on PHP.
  • Are PHP and JavaScript the same? No. PHP runs on the server. JavaScript starts in the browser.
  • Do you need HTML to learn PHP? Yes when you output a page. Less so for a pure API.