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

Generate random 5 characters string

I want to create exact 5 random characters string with least possibility of getting duplicated. What would be the best way to do it? Thanks.
Peter
  • 6,147
  • 7
  • 25
  • 15
110
votes
19 answers

mysqli_real_connect(): (HY000/2002): No such file or directory

mysqli_real_connect(): (HY000/2002): No such file or directory PhpMyAdmin error on MacOS. I want answer I really have no idea what I need to do to resolve this.
luigi
  • 1,111
  • 2
  • 7
  • 3
110
votes
2 answers

Send email with PHPMailer - embed image in body

I'm trying to send HTML mail, with PHPMailer, with images. The body is loaded from a html file, that contains all the info. When sending the mail, the image does not appear in the body, although I even send the image also as an attachment. HTML…
elvispt
  • 4,832
  • 7
  • 27
  • 35
110
votes
9 answers

How do I immediately execute an anonymous function in PHP?

In JavaScript, you can define anonymous functions that are executed immediately: (function () { /* do something */ })() Can you do something like that in PHP?
Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201
110
votes
15 answers

dyld: Library not loaded: /usr/local/lib/libjpeg.8.dylib - homebrew php

PHP was not working for me as I was encountering this libpng issue, so I reinstalled a new version with Homebrew. However, I'm getting a similar error with libjpeg this time: $ php -v dyld: Library not loaded: /usr/local/lib/libjpeg.8.dylib …
Snowcrash
  • 80,579
  • 89
  • 266
  • 376
110
votes
4 answers

Composer - the requested PHP extension mbstring is missing from your system

I've recently tried to install package through Composer, but I have got an error the requested PHP extension mbstring is missing from your system. I removed semicolon from php.ini, but it still doesn't work. What should I do?
Jozef Cipa
  • 2,133
  • 3
  • 15
  • 29
110
votes
10 answers

How to add a new method to a php object on the fly?

How do I add a new method to an object "on the fly"? $me= new stdClass; $me->doSomething=function () { echo 'I\'ve done something'; }; $me->doSomething(); //Fatal error: Call to undefined method stdClass::doSomething()
MadBad
  • 1,203
  • 2
  • 9
  • 7
110
votes
11 answers

CodeIgniter - accessing $config variable in view

Pretty often I need to access $config variables in views. I know I can pass them from controller to load->view(). But it seems excessive to do it explicitly. Is there some way or trick to access $config variable from CI views without disturbing…
AlexA
  • 4,028
  • 8
  • 51
  • 84
110
votes
12 answers

Checking for empty arrays: count vs empty

This question on 'How to tell if a PHP array is empty' had me thinking of this question Is there a reason that count should be used instead of empty when determining if an array is empty or not? My personal thought would be if the 2 are equivalent…
Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
110
votes
5 answers

In PHP, how to detect the execution is from CLI mode or through browser ?

I have a common script which Im including in my PHPcron files and the files which are accessing through the browser. Some part of the code, I need only for non cron files. How can I detect whether the execution is from CLI or through browser (I know…
binoy
  • 1,758
  • 3
  • 21
  • 42
110
votes
8 answers

Date minus 1 year?

I've got a date in this format: 2009-01-01 How do I return the same date but 1 year earlier?
ajsie
  • 77,632
  • 106
  • 276
  • 381
110
votes
10 answers

how to configuring a xampp web server for different root directory

The web directory is the home of all of your application's public and static files. Including images, stylesheets and JavaScript files. It is also where the front controllers live. So the question is when i install and set up xampp my web directory…
vardius
  • 6,326
  • 8
  • 52
  • 97
110
votes
27 answers

Apache is downloading php files instead of displaying them

OS and server information: CentOS 6.4 (Final) Apache 2.2.15 PHP 5.5.1 I previously had php 5.3.x installed but decided to upgrade. I first uninstalled the php 5.3.x and then installed php 5.5.1 but after the installation completed apache did not…
Anthony Gainor
  • 1,149
  • 2
  • 10
  • 9
110
votes
10 answers

What are .tpl files? PHP, web design

Someone wants me to redesign a site run in PHP (VideoCMS). But when I asked him to send me the source he has given me *.tpl files instead of *.php. There is some code inside them: {include file='header.tpl' p="article"}
Dan
  • 55,715
  • 40
  • 116
  • 154
110
votes
16 answers

How to round up a number to nearest 10?

How can we round off a number to the nearest 10 in php? Say I have 23, what code would I use to round it off to 30?
tarnfeld
  • 25,992
  • 41
  • 111
  • 146