Questions tagged [php]

PHP is an open source, multi-paradigm, dynamically-typed and interpreted scripting language designed initially for server-side web development. Use this tag for questions about programming in the PHP language.

PHP is a widely used, open source, general-purpose, multi-paradigm, dynamically typed and interpreted scripting language designed initially for server-side web development.

The original PHP project, as invented by Rasmus Lerdorf, stood for Personal Home Page. Today, it stands for the recursive acronym PHP: Hypertext Preprocessor.

The latest stable release, development changes, and development branches can be found on the PHP website, and the source code, written in C, is available at PHP's GitHub repository.

To create an environment for learning and experimenting with PHP, several application bundles include – among other components – a web server and PHP:

There are options like Cygwin (Linux on Windows), in which you can install PHP just like any other Linux

PHP provides a built-in web server for testing and development purposes. It can be started using the following command:

 php -S localhost:8000

After executing the above command, the server will listen on port 8000 using the current working directory as its document root. See the PHP manual for more information.

Notice: To make an online demo for your question, you may use codepad, 3v4l, or PHP Sandbox. However, your question or answer should still include all relevant codes.

PHP Versions

Current Stable Version (8.2.x) : 8.2.7 // Release Date: 08 Jun 2023

Old Stable Version (8.1.x) : 8.1.20 // Release Date: 08 Jun 2023

Old Stable Version (8.0.x) : 8.0.29 // Release Date: 08 Jun 2023

It is recommended to use the current stable released version. All versions below 8.0 are officially unsupported and have been announced end-of-life. A list of supported branches and their maintenance status can be found here.

For further information about new features and required changes in a new version, see the official migration docs:

Sample PHP script

This script displays Hello World! on your screen.

<?php

echo 'Hello World!';

To run this script in a console, save it in the current working directory in a file called hello.php and execute the command: php hello.php.

Community

PHP has many active community forums, including:

More information

Online documentation

The PHP manual is the official documentation for the language syntax featuring function search and URL shortcuts (for example, https://php.net/explode). The API is well documented for bundled and additional extensions. Most additional extensions can be found in PECL. The PEAR repository contains a plethora of community supplied classes. It is also possible to download an offline version of the documentation here.

The PHP Framework Interop Group (PHP-FIG) has also created standards concerning PHP coding styles and standards. These PHP Standard Recommendations (PSRs) can be found here.

PHP Tutorials

PHP security-related information

Free PHP Programming Books

Database support

PHP supports a wide range of databases, relational and non-relational alike.

PHP is often paired with the MySQL relational database. PHP also includes great database support for PostgreSQL, SQLite, Microsoft SQL Server (API reference), Oracle, IBM DB2 & Cloudscape, Apache Derby, and even ODBC.

All modern versions of PHP include PDO: a built-in data-access abstraction library with comprehensive connectivity options. More recently, PECL extensions that offer "NoSQL" database support have surfaced, including Apache Thrift (for Apache Cassandra), MongoDB, Redis, and others.

Useful Third-party Code and Tools

In addition to the extensive functionality provided in the PHP Core and through PEAR and PECL, there are several noteworthy third-party contributions to the PHP world, some of which are listed below:

Package Management with Composer

Composer is a package management tool for PHP inspired by npm for Node.js and Bundler for Ruby. It allows for per-project dependencies to be specified in a JSON file.

The Composer uses packages from Packagist, rapidly growing to contain many of the most popular PHP libraries.

Composer solves the following problems:

  1. You have a project that depends on several libraries.
  2. Some of those libraries depend on other libraries.
  3. You declare the things you depend on.
  4. Composer determines which versions of which packages need to be installed and downloads them into a directory (usually vendor) in your project.

Nothing comes for free. Software downloaded with Composer may have bugs like any other, including security vulnerabilities. You are responsible for being aware of what you install and updating when necessary to get security fixes.

Quality Assurance Tools

Coding standards and conventions

Several coding standards have been proposed and accepted by the PHP Framework Interop Group (PHP-FIG). These are known as the PHP Standards Recommendations (PSRs). As of July 2nd, 2017, the following recommendations are in effect:

A complete list of all recommendations alongside their status can be found on the PHP-FIG Recommendations page


Reference

Official Logo:

php logo php alternate logo


1461764 questions
110
votes
10 answers

PHP, Get tomorrows date from date

I have a PHP date in the form of 2013-01-22 and I want to get tomorrows date in the same format, so for example 2013-01-23. How is this possible with PHP?
Justin
  • 42,716
  • 77
  • 201
  • 296
110
votes
20 answers

How do I close a connection early?

I'm attempting to do an AJAX call (via JQuery) that will initiate a fairly long process. I'd like the script to simply send a response indicating that the process has started, but JQuery won't return the response until the PHP script is done…
Eric_WVGG
  • 2,957
  • 3
  • 28
  • 29
110
votes
9 answers

adding 1 day to a DATETIME format value

In certain situations I want to add 1 day to the value of my DATETIME formatted variable: $start_date = date('Y-m-d H:i:s', strtotime("{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}")); What is the best way to do this?
ian
  • 11,605
  • 25
  • 69
  • 96
110
votes
7 answers

Why does PHP convert a string with the letter E into a number?

Why does the following statement return true? "608E-4234" == "272E-3063" I have also tried this with single quotes around the strings. The only way I can get it to evaulate to false is by using the === operator instead of == My guess is PHP is…
Andy
  • 4,901
  • 5
  • 35
  • 57
110
votes
6 answers

What does "=>" mean in PHP?

What does the => operator mean in the following code? foreach ($user_list as $user => $pass) The code is a comment at PHP.net. The user does not specify the value of $user_list, $user or $pass. I normally see that => means equal or greater…
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
110
votes
31 answers

Is there a pretty print for PHP?

I'm fixing some PHP scripts and I'm missing ruby's pretty printer. i.e. require 'pp' arr = {:one => 1} pp arr will output {:one => 1}. This even works with fairly complex objects and makes digging into an unknown script much easier. Is there some…
Aaron Lee
  • 5,287
  • 5
  • 24
  • 19
109
votes
8 answers

Test PHP headers with PHPUnit

I'm trying to use PHPunit to test a class that outputs some custom headers. The problem is that on my machine this:
titel
  • 3,454
  • 9
  • 45
  • 54
109
votes
22 answers

Fatal error: Maximum execution time of 300 seconds exceeded

I keep getting this PHP error: Fatal error: Maximum execution time of 300 seconds exceeded I have tried setting my max_execution_time and my max_input_time settings in php.ini (both apache and cli) to 0, -1 and 4000 seconds each. And i still get…
dez
  • 2,195
  • 6
  • 25
  • 29
109
votes
12 answers

select count(*) from table of mysql in php

I am able to get both the value and row of the mysql query result. But I am struggling to get the single output of a query. e.g.: $result = mysql_query("SELECT COUNT(*) FROM Students;"); I need the result to display. But I am not getting the…
Gana
  • 1,093
  • 2
  • 8
  • 4
109
votes
2 answers

When to implement and extend?

When should implement or extend be used? What are some real-world examples? Is this correct? Implementing appears to be a way to enforce that certain methods exists in a class, and that these methods function calls are properly formatted.…
Industrial
  • 41,400
  • 69
  • 194
  • 289
109
votes
12 answers

How can I update code that uses the deprecated each() function?

With PHP 7.2, each is deprecated. The documentation says: Warning This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged. How can I update my code to avoid using it? Here are some examples: $ar =…
yokogeri
  • 1,217
  • 2
  • 8
  • 11
109
votes
28 answers

How to set php executable path php.validate.executablePath in vscode when php is set inside docker container?

I have a development environment based in docker. Everytime that I open VSCode I get this message: Cannot validate since no PHP executable is set. Use the setting 'php.validate.executablePath' to configure the PHP executable. Somebody know how…
Nicolas Takashi
  • 1,550
  • 2
  • 10
  • 11
109
votes
23 answers

Extension gd is missing from your system - laravel composer Update

I newly install Dompdf in Laravel Project via Composer (composer require barryvdh/laravel-dompdf). After enter the Command Terminal Reply Following Errors. Problem 1 - dompdf/dompdf v0.7.0 requires ext-gd * -> the requested PHP extension gd is…
Karthik
  • 5,589
  • 18
  • 46
  • 78
109
votes
12 answers

Push item to associative array in PHP

I've been trying to push an item to an associative array like this: $new_input['name'] = array( 'type' => 'text', 'label' => 'First name', 'show' => true, 'required' => true ); array_push($options['inputs'], $new_input); However,…
ryudice
  • 36,476
  • 32
  • 115
  • 163
109
votes
11 answers

Convert a series of parent-child relationships into a hierarchical tree?

I have a bunch of name-parentname pairs, that I'd like to turn into as few heirarchical tree structures as possible. So for example, these could be the pairings: Child : Parent H : G F : G G : D E : D A : E B : C C : E …
Eric
  • 95,302
  • 53
  • 242
  • 374