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.


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.


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, 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


Official Logo:

php logo php alternate logo

1461764 questions
25 answers

Reference Guide: What does this symbol mean in PHP? (PHP Syntax)

What is this? This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list. Why is this? It used to be hard to find questions…
  • 312,688
  • 75
  • 539
  • 559
25 answers

Deleting an element from an array in PHP

Is there an easy way to delete an element from an array using PHP, such that foreach ($array) no longer includes that element? I thought that setting it to null would do it, but apparently it does not work.
  • 66,838
  • 37
  • 84
  • 108
31 answers

How do I get a YouTube video thumbnail from the YouTube API?

If I have a YouTube video URL, is there any way to use PHP and cURL to get the associated thumbnail from the YouTube API?
  • 47,274
  • 54
  • 131
  • 219
27 answers

How can I prevent SQL injection in PHP?

If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example: $unsafe_variable = $_POST['user_input']; mysql_query("INSERT INTO `table` (`column`) VALUES…
Andrew G. Johnson
  • 26,603
  • 30
  • 91
  • 135
14 answers

Why shouldn't I use mysql_* functions in PHP?

What are the technical reasons for why one shouldn't use mysql_* functions? (e.g. mysql_query(), mysql_connect() or mysql_real_escape_string())? Why should I use something else even if they work on my site? If they don't work on my site, why do I…
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
36 answers

How do I check if a string contains a specific word?

Consider: $a = 'How are you?'; if ($a contains 'are') echo 'true'; Suppose I have the code above, what is the correct way to write the statement if ($a contains 'are')?
Charles Yeung
  • 38,347
  • 30
  • 90
  • 130
31 answers

How do you parse and process HTML/XML in PHP?

How can one parse HTML/XML and extract information from it?
  • 56,863
  • 21
  • 114
  • 161
7 answers

How does PHP 'foreach' actually work?

Let me prefix this by saying that I know what foreach is, does and how to use it. This question concerns how it works under the bonnet, and I don't want any answers along the lines of "this is how you loop an array with foreach". For a long time I…
  • 87,921
  • 11
  • 154
  • 174
24 answers

When should I use 'self' over '$this'?

In PHP 5, what is the difference between using self and $this? When is each appropriate?
Casey Watson
  • 51,574
  • 10
  • 32
  • 30
27 answers

How do I get PHP errors to display?

I have checked my PHP ini file (php.ini) and display_errors is set and also error reporting is E_ALL. I have restarted my Apache webserver. I have even put these lines at the top of my script, and it doesn't even catch simple parse errors. For…
  • 56,052
  • 101
  • 275
  • 409
30 answers

How Can I add HTML And CSS Into PDF

I have an HTML (not XHTML) document that renders fine in Firefox 3 and IE 7. It uses fairly basic CSS to style it and renders fine in HTML. I'm now after a way of converting it to PDF. I have tried: DOMPDF: it had huge problems with tables. I…
  • 616,129
  • 168
  • 910
  • 942
34 answers

startsWith() and endsWith() functions in PHP

How can I write two functions that would take a string and return if it starts with the specified character/string or ends with it? For example: $str = '|apples}'; echo startsWith($str, '|'); //Returns true echo endsWith($str, '}'); //Returns true
  • 261,656
  • 265
  • 575
  • 769
35 answers

How do I make a redirect in PHP?

Is it possible to redirect a user to a different page through the use of PHP? Say the user goes to and I want to redirect them to, how would I do so without the use of a meta refresh? Is it…
  • 18,417
  • 5
  • 22
  • 11
28 answers

Difference between require, include, require_once and include_once?

In PHP: When should I use require vs. include? When should I use require_once vs. include_once?
Scott B
  • 38,833
  • 65
  • 160
  • 266
36 answers

How to get the client IP address in PHP

How can I get the client IP address using PHP? I want to keep record of the user who logged into my website through his/her IP address.
Anup Prakash
  • 14,406
  • 4
  • 17
  • 12
2 3
99 100