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
108
votes
6 answers

How do you pull first 100 characters of a string in PHP

I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing. Is there a function that can do this easily? For example: $string1 = "I am looking for a way to pull the first 100 characters…
JoshFinnie
  • 4,841
  • 7
  • 28
  • 28
108
votes
7 answers

PHP YAML Parsers

Does anyone know of a good YAML Parser for PHP? If so, what are the pros and cons of this library?
sgibbons
  • 3,620
  • 11
  • 36
  • 31
108
votes
10 answers

Yii2 data provider default sorting

In Yii 1.1 this code works for default sorting: $dataProvider = new CActiveDataProvider('article',array( 'sort'=>array( 'defaultOrder'=>'id DESC', ), )); How default sorting can be set in Yii2? Tried below code, but no…
Sarvar Nishonboyev
  • 12,262
  • 10
  • 69
  • 70
108
votes
7 answers

When and why I should use session_regenerate_id()?

Why and when should I use the session_regenerate_id() function in php? Should I always use it after I use the session_start()? I've read that I have to use it to prevent session fixation, is this the only reason?
rvandoni
  • 3,297
  • 4
  • 32
  • 46
108
votes
16 answers

php convert datetime to UTC

I am in need of an easy way to convert a date time stamp to UTC (from whatever timezone the server is in) HOPEFULLY without using any libraries.
Jeremey
  • 1,456
  • 3
  • 13
  • 19
108
votes
5 answers

Has Facebook sharer.php changed to no longer accept detailed parameters?

We have been opening a sharing popup (via window.open) with the URL like https://www.facebook.com/sharer/sharer.php?s=100&p[title]=EXAMPLE&p[summary]=EXAMPLE&p[url]=EXAMPLE&p[images][0]=EXAMPLE and until some unknown point in the last month or so…
tdous
  • 1,599
  • 2
  • 14
  • 19
108
votes
11 answers

How to get innerHTML of DOMNode?

What function do you use to get innerHTML of a given DOMNode in the PHP DOM implementation? Can someone give reliable solution? Of course outerHTML will do too.
Dawid Ohia
  • 16,129
  • 24
  • 81
  • 95
108
votes
11 answers

How to create a logfile in php

I want to create a logfile for my system to register/log every action they do inside the system. But I have no idea how to do it. For example, I have this php code that does the login function. public function hasAccess($username,$password){ …
maecy m
  • 1,287
  • 3
  • 15
  • 20
108
votes
2 answers

Why is the standard session lifetime 24 minutes (1440 seconds)?

I've been doing some research on PHP Session Handling and came across the session.gc_maxlifetime value of 1440 seconds. I've been wondering why the standard value is 1440 and how it is calculated? What is the basis for this calculation? How long…
Anna Völkl
  • 1,664
  • 2
  • 15
  • 28
108
votes
4 answers

what is faster: in_array or isset?

This question is merely for me as I always like to write optimized code that can run also on cheap slow servers (or servers with A LOT of traffic) I looked around and I was not able to find an answer. I was wondering what is faster between those…
Fabrizio
  • 3,734
  • 2
  • 29
  • 32
108
votes
9 answers

Too much data with var_dump in symfony2 doctrine2

I have around 40 entities and many bidirectional relationships. Whenever i use var_dump($user) or any entity my browser gets loaded with too much data of arrays and variables then it just crashed. i want to whats the problem. The data is being…
Mirage
  • 30,868
  • 62
  • 166
  • 261
107
votes
13 answers

Diagnosing Memory Leaks - Allowed memory size of # bytes exhausted

I've encountered the dreaded error-message, possibly through-painstaking effort, PHP has run out of memory: Allowed memory size of #### bytes exhausted (tried to allocate #### bytes) in file.php on line 123 Increasing the limit If you know what…
Mike B
  • 31,886
  • 13
  • 87
  • 111
107
votes
2 answers

Accessing outside variable using anonymous function as params

Basically I use this handy function to processing db rows (close an eye on PDO and/or other stuff) function fetch($query,$func) { $query = mysql_query($query); while($r = mysql_fetch_assoc($query)) { $func($r); } } With this…
dynamic
  • 46,985
  • 55
  • 154
  • 231
107
votes
7 answers

Get all variables sent with POST?

I need to insert all variables sent with post, they were checkboxes each representing a user. If I use GET I get something like this: ?19=on&25=on&30=on I need to insert the variables in the database. How do I get all variables sent with POST? As…
lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
107
votes
11 answers

Merge two numerically-keyed associative arrays and preserve the original keys

I have two arrays like this: array( '11' => '11', '22' => '22', '33' => '33', '44' => '44' ); array( '44' => '44', '55' => '55', '66' => '66', '77' => '77' ); I want to combine these two array such that it does not contains duplicate and as well…
Awan
  • 18,096
  • 36
  • 89
  • 131
1 2 3
99
100