We are still actively working on the spam issue.

PHP

From InstallGentoo Wiki
Revision as of 22:37, 2 October 2019 by Sidekek (talk | contribs) (spelling)
Jump to: navigation, search
/g/ and /tech/'s most mixed scripted language when mentioned.

PHP (PHP Hypertext Preprocessor) is a server-side programming language that is very popular and garnered a poor reputation during the early and mid 2010s. The acronym PHP was originally Personal Homepage, later to have its named changed to the more informative PHP Hypertext Preprocessor. It uses a C style syntax and even some basic C functions. PHP is very easy to learn and use; can make small to medium web projects but will require a deep familiarity with the language to create large/enterprise level applications without things turning into a giant mess. There has been a recent effort by PHP contributors and evangelists to 'fix' PHP's reputation.

Known projects written in PHP:

  • Facebook - Most visited botnet social networking site. Used a custom implementation that converted PHP to C. Has made an active effort to move away from PHP
  • Yahoo - Recently PHP but slowly transitioning into node.js.
  • Wordpress - Popular blog CMS (which also offer web hosting services). Also the largest contributor to the negative connotations associated with PHP.
  • Drupal - Another popular CMS that powers many sites
  • VirtualBB & phpBB - Two extremely popular forum frameworks that have powered some extremely popular forums over the years
  • MediaWiki - The Wiki software that powers this website

Recent Developments

Due to the overwhelming negative opinion by the web development industry, various community members have banded together in an attempt to fix the most common complaints with PHP, as well as to increase interoperability by adopting standards. For instance, the PHP Standards Recommendations (PSRs) aim to define a standardized way for various items such as code style, autoloading, middleware stacks, and http response handling.

PHP 7

With the release of PHP 7.0 in December of 2015, the language provided a vast number of new features and improvements over PHP 5. Various benchmarks saw up to a 4x increase in performance over PHP 5.6, and a 20x increase over PHP < 5.5, putting it on par with other web-focused languages. PHP 7 also introduced return types and strict type checking which can help to identify bugs with unexpected data types. A full list of new features in PHP 7 can be seen here.

PHP Standards Recommendation

The PHP Standards Recommendation is an effort undertaken by the PHP Framework Interoperability Group. Their aim with PSRs "...is for project representatives to talk about the commonalities between our projects and find ways we can work together." More information on the various PSRs (as well as information on the group themselves) is available on the PHP-FIG website. Adherence to PSRs is now commonplace for almost all PHP-based frameworks, and should be considered when undertaking a PHP project of any size.

Frameworks

A framework provides a lot of the base systems that are usually required to build any kind of substantial web application. Most modern PHP frameworks implement the Model-View-Controller (MVC) design pattern, though there are some exceptions. Below is a list of popular frameworks (Note: you should pay special attention to the licenses and specific use cases of these frameworks before using them):

Examples

PHP can be written inline with HTML documents, making it ideal for people who have experience with designing websites with HTML/CSS and want to create a more dynamic web experience. However, modern PHP frameworks have moved away from using raw PHP to using templating libraries such as Blade (as part of the Laravel framework), Twig, and Smarty. PHP is processed by the server before it is rendered in the browser.

Below are some examples of using PHP inline in an HTML document. All of the examples here assume that you have a rudimentary understanding of HTML and have web server software installed on your system, or access to a web server with PHP.

The PHP documentation is a valuable resource that should always be open in a separate tab when you're developing PHP projects.

Hello World!

   <?php
       echo "Hello world!";
   ?>
   <?="Hello world!";?>


If you put this code into a file and give it a .php extension, it will render plain text to the web browser. If you embed PHP code in an HTML document, you must include the closing ?> tag, but if you have a pure PHP file the closing tag can and should be omitted (prevents extra newlines from being erroneously added and other errata; see [1]).

Date

   <?php
       // variable declaration
       $date = date("Y/m/d");
   
       // using the variable with the echo function
       echo $date;
   ?>

The Date function is a great tool to show how PHP generates dynamic content. The above format displays the full year, the month and day all in numbers like so: 1999/02/16.

Set Up

Cross Platform

XAMPP is one solution for easy setup if you're that lazy.

Linux/BSD

Most package managers have a current version (7.x) of PHP being maintained. Server software needs specific tools to allow PHP to be executed on the system.

Apache

On a Debian based distribution, you must install the Apache web server, the PHP module and PHP itself. The below command will install apache2 and PHP 7.0. The current (as of 2019-09-27) version of PHP is 7.3, so it is recommended that you add the PHP 7.3 PPA repository and install PHP 7.3.

   $ sudo apt-get install apache2 libapache2-mod-php php

Nginx

Nginx requires you to have php-fpm or php-cgi installed and configured. This will show you how to install and configure php-fpm with nginx.

Windows

You could, if you're silly, download WAMPserver but that is generally not the best move. However, if you were serious about PHP development you would use a dedicated server.

Software