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

What are the differences in die() and exit() in PHP?

What are the differences between die() and exit() functions in PHP? I think both have the same functionality, but I doubt there is something different in both... what is it?
coderex
  • 27,225
  • 45
  • 116
  • 170
762
votes
4 answers

How do I strip all spaces out of a string in PHP?

How can I strip / remove all spaces of a string in PHP? I have a string like $string = "this is my string"; The output should be "thisismystring" How can I do that?
streetparade
  • 32,000
  • 37
  • 101
  • 123
754
votes
21 answers

Create a folder if it doesn't already exist

I've run into a few cases with WordPress installs with Bluehost where I've encountered errors with my WordPress theme because the uploads folder wp-content/uploads was not present. Apparently the Bluehost cPanel WordPress installer does not create…
Scott B
  • 38,833
  • 65
  • 160
  • 266
736
votes
7 answers

Are PDO prepared statements sufficient to prevent SQL injection?

Let's say I have code like this: $dbh = new PDO("blahblah"); $stmt = $dbh->prepare('SELECT * FROM users where username = :username'); $stmt->execute( array(':username' => $_REQUEST['username']) ); The PDO documentation says: The parameters to…
Mark Biek
  • 146,731
  • 54
  • 156
  • 201
732
votes
11 answers

Formatting a number with leading zeros in PHP

I have a variable which contains the value 1234567. I would like it to contain exactly 8 digits, i.e. 01234567. Is there a PHP function for that?
Andromeda
  • 12,659
  • 20
  • 77
  • 103
703
votes
17 answers

Error "Cannot use object of type stdClass as array"

I get a strange error using json_decode(). It decodes the data correctly (I saw it using print_r), but when I try to access to information inside the array I get: Fatal error: Cannot use object of type stdClass as array…
Dail
  • 7,039
  • 3
  • 15
  • 3
694
votes
5 answers

How to check if a string starts with a specified string?

I'm trying to check if a string starts with http. How can I do this check? $string1 = 'google.com'; $string2 = 'http://www.google.com';
Andrew
  • 227,796
  • 193
  • 515
  • 708
673
votes
13 answers

How can I capture the result of var_dump to a string?

I'd like to capture the output of var_dump to a string. The PHP documentation says; As with anything that outputs its result directly to the browser, the output-control functions can be used to capture the output of this function, and save it in a…
Mark Biek
  • 146,731
  • 54
  • 156
  • 201
657
votes
23 answers

Sort array of objects by one property

How can I sort this array of objects by one of its fields, like name or count? Array ( [0] => stdClass Object ( [ID] => 1 [name] => Mary Jane [count] => 420 ) [1] => stdClass Object …
Alex
  • 66,732
  • 177
  • 439
  • 641
656
votes
20 answers

Preferred method to store PHP arrays (json_encode vs serialize)

I need to store a multi-dimensional associative array of data in a flat file for caching purposes. I might occasionally come across the need to convert it to JSON for use in my web app but the vast majority of the time I will be using the array…
KyleFarris
  • 17,274
  • 5
  • 40
  • 40
654
votes
9 answers

How to add elements to an empty array in PHP?

If I define an array in PHP such as (I don't define its size): $cart = array(); Do I simply add elements to it using the following? $cart[] = 13; $cart[] = "foo"; $cart[] = obj; Don't arrays in PHP have an add method, for example, cart.add(13)?
AquinasTub
  • 8,435
  • 6
  • 22
  • 15
647
votes
14 answers

How to find the foreach index?

Is it possible to find the foreach index? in a for loop as follows: for ($i = 0; $i < 10; ++$i) { echo $i . ' '; } $i will give you the index. Do I have to use the for loop or is there some way to get the index in the foreach loop?
user18334
  • 6,563
  • 2
  • 19
  • 7
644
votes
41 answers

How can I get useful error messages in PHP?

Quite often I will try and run a PHP script and just get a blank screen back. No error message; just an empty screen. The cause might have been a simple syntax error (wrong bracket, missing semicolon), or a failed function call, or something else…
Candidasa
  • 8,580
  • 10
  • 30
  • 31
642
votes
11 answers

PHP expects T_PAAMAYIM_NEKUDOTAYIM?

Does anyone have a T_PAAMAYIM_NEKUDOTAYIM?
Peter Turner
  • 11,199
  • 10
  • 68
  • 109
641
votes
7 answers

How to install a specific version of package using Composer?

I am trying to install a specific version of a package using Composer. I tried composer install and composer require but they are installing the latest version of the package. What if I want an older version?
gdaras
  • 9,401
  • 2
  • 23
  • 39