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
111
votes
8 answers

Dynamic class method invocation in PHP

Is there a way to dynamically invoke a method in the same class for PHP? I don't have the syntax right, but I'm looking to do something similar to this: $this->{$methodName}($arg1, $arg2, $arg3);
VirtuosiMedia
  • 52,016
  • 21
  • 93
  • 140
111
votes
3 answers

Detect EXIF Orientation and Rotate Image using ImageMagick

Canon DSLRs appear to save photos in landscape orientation and uses exif::orientation to do the rotation. Question: How can imagemagick be used to re-save the image into the intended orientation using the exif orientation data such that it no longer…
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
111
votes
2 answers

PHP shorthand for isset()?

Is there a shorthand way to assign a variable to something if it doesn't exist in PHP? if(!isset($var) { $var = ""; } I'd like to do something like $var = $var | "";
brentonstrine
  • 21,694
  • 25
  • 74
  • 120
111
votes
13 answers

How do I use PHP namespaces with autoload?

I get this error when I try to use autoload and namespaces: Fatal error: Class 'Class1' not found in /usr/local/www/apache22/data/public/php5.3/test.php on line 10 Can anyone tell me what I am doing wrong? Here is my…
David Barnes
  • 2,138
  • 5
  • 19
  • 25
111
votes
8 answers

Get operating system info

I recently started wondering about sites like http://thismachine.info/ that get the user's operating system info. I have not been able to find out how to do that with PHP, and wanted to try to figure it out. I noticed that they list the user-agent,…
pattyd
  • 5,927
  • 11
  • 38
  • 57
111
votes
7 answers

PDO mysql: How to know if insert was successful

I'm using PDO to insert a record (mysql and php) $stmt->bindParam(':field1', $field1, PDO::PARAM_STR); $stmt->bindParam(':field2', $field2, PDO::PARAM_STR); $stmt->execute(); Is there a way to know if it inserted successfully, for example if the…
Chris
  • 8,736
  • 18
  • 49
  • 56
111
votes
6 answers

Installing PDO driver on MySQL Linux server

I was suggested, not long ago, to change my code to use PDO in order to parameterize my queries and safely save HTML in the database. Well, here are the main problems: I looked at http://php.net/manual/en/ref.pdo-mysql.php, and I don't really get…
Yuri Scarbaci
  • 1,475
  • 2
  • 11
  • 13
111
votes
15 answers

If isset $_POST

I have a form on one page that submits to another page. There, it checks if the input mail is filled. If so then do something and if it is not filled, do something else. I don't understand why it always says that it is set, even if I send an empty…
Nrc
  • 9,577
  • 17
  • 67
  • 114
111
votes
2 answers

multiple ways of calling parent method in php

At first I was confused why both of the method calls in the constructor work, but now I think I understand. The extending classes inherit the parent's methods as if they were declared in the class itself, AND the methods exist in the parent, so both…
jerry
  • 2,743
  • 9
  • 41
  • 61
110
votes
11 answers

Adding three months to a date in PHP

I have a variable called $effectiveDate containing the date 2012-03-26. I am trying to add three months to this date and have been unsuccessful at it. Here is what I have tried: $effectiveDate = strtotime("+3 months",…
user979331
  • 11,039
  • 73
  • 223
  • 418
110
votes
7 answers

How to use phpexcel to read data and insert into database?

I have a php application where I want to read data from excel, Insert into database and then generate pdf reports for specific users. I searched a lot but nothing specific given about both things.
coder101
  • 1,601
  • 2
  • 21
  • 41
110
votes
6 answers

$PHP_AUTOCONF errors on mac os x 10.7.3 when trying to install pecl extensions

I am trying to setup my machine with pecl_http and memcache and in both cases, I get similar errors. This is on MAC OS X 10.7.3 (lion) and I also have XCODE installed on it. I also installed Zend Server community edition before running these…
krishna
  • 3,547
  • 5
  • 27
  • 29
110
votes
6 answers

php static function

I have a question regarding static function in php. let's assume that I have a class class test { public function sayHi() { echo 'hi'; } } if I do test::sayHi(); it works without a problem. class test { public static function…
Moon
  • 22,195
  • 68
  • 188
  • 269
110
votes
7 answers

Delimiter must not be alphanumeric or backslash and preg_match

I have this code : $string1 = "My name is 'Kate' and im fine"; $pattern = "My name is '(.*)' and im fine"; preg_match($pattern , $string1, $matches); echo $matches[1]; and when im run it returns this error: Warning: preg_match()…
user547363
  • 1,189
  • 2
  • 8
  • 7
110
votes
7 answers

Comment out HTML and PHP together

I have this code,
Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321