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
1117
votes
17 answers

How do I use PHP to get the current year?

I want to put a copyright notice in the footer of a web site, but I think it's incredibly tacky for the year to be outdated. How would I make the year update automatically with PHP
JD Graffam
  • 11,275
  • 2
  • 18
  • 6
1081
votes
14 answers

Detecting request type in PHP (GET, POST, PUT or DELETE)

How can I detect which request type was used (GET, POST, PUT or DELETE) in PHP?
UnkwnTech
  • 88,102
  • 65
  • 184
  • 229
1070
votes
14 answers

Where can I find php.ini?

A few years ago I installed Apache 2.2x and PHP 5.3.1 on a Linux server I maintain. I used .tar.gz's and built them as instructed (instead of rpms and what-have-you). And all was fine. Today I needed to install this which seems like a PHP library. …
necromancer
  • 23,916
  • 22
  • 68
  • 115
1052
votes
42 answers

How do I get the current date and time in PHP?

Which PHP function can return the current date/time?
Mike
1021
votes
27 answers

Remove empty array elements

Some elements in my array are empty strings from users. $linksArray still has empty elements after the following: foreach($linksArray as $link) { if($link == '') { unset($link); } } print_r($linksArray); The empty() function…
Will
  • 10,731
  • 6
  • 22
  • 16
1003
votes
69 answers

PHP random string generator

I'm trying to create a randomized string in PHP, and I get absolutely no output with this:
Captain Lightning
  • 10,493
  • 4
  • 19
  • 17
1002
votes
31 answers

mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource

I am trying to select data from a MySQL table, but I get one of the following error messages: mysql_fetch_array() expects parameter 1 to be resource, boolean given This is my code: $username = $_POST['username']; $password =…
iamjonesy
  • 24,732
  • 40
  • 139
  • 206
984
votes
34 answers

Convert a PHP object to an associative array

I'm integrating an API to my website which works with data stored in objects while my code is written using arrays. I'd like a quick-and-dirty function to convert an object to an array.
Haroldo
  • 36,607
  • 46
  • 127
  • 169
975
votes
27 answers

Get the full URL in PHP

I use this code to get the full URL: $actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; The problem is that I use some masks in my .htaccess, so what we see in the URL is not always the real path of the file. What I need is to get…
DiegoP.
  • 45,177
  • 34
  • 89
  • 107
930
votes
5 answers

What is thread safe or non-thread safe in PHP?

I saw different binaries for PHP, like non-thread or thread safe? What does this mean? What is the difference between these packages?
O..
  • 10,925
  • 5
  • 20
  • 8
922
votes
7 answers

What is the difference between single-quoted and double-quoted strings in PHP?

I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes. I just know in .NET, or the C language, if it is in a single quote, that means it is a character, not a string.
rob waminal
  • 18,117
  • 17
  • 50
  • 64
916
votes
35 answers

How do I convert a string to a number in PHP?

I want to convert these types of values, '3', '2.34', '0.234343', etc. to a number. In JavaScript we can use Number(), but is there any similar method available in PHP? Input Output '2' 2 '2.34' 2.34 '0.3454545' …
Sara
  • 14,098
  • 13
  • 34
  • 50
911
votes
60 answers

How to check if PHP array is associative or sequential?

PHP treats all arrays as associative, so there aren't any built in functions. Can anyone recommend a fairly efficient way to check if an array "is a list" (contains only numeric keys starting from 0)? Basically, I want to be able to differentiate…
Wilco
  • 32,754
  • 49
  • 128
  • 160
854
votes
31 answers

How to get a file's extension in PHP?

This is a question you can read everywhere on the web with various answers: $ext = end(explode('.', $filename)); $ext = substr(strrchr($filename, '.'), 1); $ext = substr($filename, strrpos($filename, '.') + 1); $ext =…
Bite code
  • 578,959
  • 113
  • 301
  • 329
831
votes
11 answers

How to fix "Headers already sent" error in PHP

When running my script, I am getting several errors like this: Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23 The lines mentioned in the error messages contain…
Moses89
  • 4,367
  • 3
  • 15
  • 6