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
641
votes
19 answers

Is there a function to make a copy of a PHP array to another?

Is there a function to make a copy of a PHP array to another? I have been burned a few times trying to copy PHP arrays. I want to copy an array defined inside an object to a global outside it.
vfclists
  • 19,193
  • 21
  • 73
  • 92
636
votes
39 answers

dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac

I installed node using homebrew (Mojave), afterwards php stoped working and if I try to run php -v I get this error: php -v dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib Referenced from: /usr/local/bin/php Reason: image…
petekaner
  • 8,071
  • 5
  • 29
  • 52
633
votes
11 answers

What is the use of the @ symbol in PHP?

I have seen uses of @ in front of certain functions, like the following: $fileHandle = @fopen($fileName, $writeAttributes); What is the use of this symbol?
sv_in
  • 13,929
  • 9
  • 34
  • 55
629
votes
23 answers

Can I bind an array to an IN() condition in a PDO query?

I'm curious to know if it's possible to bind an array of values to a placeholder using PDO. The use case here is attempting to pass an array of values for use with an IN() condition. I'd like to be able to do something like…
Andru
  • 7,011
  • 3
  • 21
  • 25
625
votes
10 answers

Converting a Postman request to curl

I am calling my Java webservice (POST request) via Postman in the following manner which works perfectly fine (i.e. I can see my records getting inserted into the database): And, here's how the contents inside the Headers(1) tab look like: Instead…
Coder
  • 6,381
  • 2
  • 9
  • 10
624
votes
15 answers

PHP, cURL, and HTTP POST example?

Can anyone show me how to do a PHP cURL with an HTTP POST? I want to send data like this: username=user1, password=passuser1, gender=1 To www.example.com I expect the cURL to return a response like result=OK. Are there any examples?
mysqllearner
  • 13,523
  • 15
  • 43
  • 43
619
votes
23 answers

Insert new item in array on any position in PHP

How can I insert a new item into an array on any position, for example in the middle of array?
kusanagi
  • 14,296
  • 20
  • 86
  • 111
616
votes
15 answers

Converting an integer to a string in PHP

Is there a way to convert an integer to a string in PHP?
kman99
  • 7,315
  • 4
  • 24
  • 20
612
votes
21 answers

PHP How to determine the first and last iteration in a foreach loop?

The question is simple. I have a foreach loop in my code: foreach($array as $element) { //code } In this loop, I want to react differently when we are in first or last iteration. How to do this?
mehdi
  • 9,262
  • 11
  • 35
  • 35
608
votes
24 answers

How to check whether an array is empty using PHP?

players will either be empty or a comma separated list (or a single value). What is the easiest way to check if it's empty? I'm assuming I can do so as soon as I fetch the $gameresult array into $gamerow? In this case it would probably be more…
aslum
  • 11,774
  • 16
  • 49
  • 70
602
votes
23 answers

Sort an array of associative arrays by column value

Given this array: $inventory = array( array("type"=>"fruit", "price"=>3.50), array("type"=>"milk", "price"=>2.90), array("type"=>"pork", "price"=>5.43), ); I would like to sort $inventory's elements by price to get: $inventory = array( …
Matt
  • 22,224
  • 25
  • 80
  • 116
600
votes
23 answers

How to create custom helper functions in Laravel

I would like to create helper functions to avoid repeating code between views in Laravel. For example: view.blade.php

Foo Formated text: {{ fooFormatText($text) }}

They're basically text formatting functions. How should I define globally…
Calebe Oliveira
  • 6,111
  • 3
  • 14
  • 7
599
votes
18 answers

How do I catch a PHP fatal (`E_ERROR`) error?

I can use set_error_handler() to catch most PHP errors, but it doesn't work for fatal (E_ERROR) errors, such as calling a function that doesn't exist. Is there another way to catch these errors? I am trying to call mail() for all errors and am…
too much php
  • 88,666
  • 34
  • 128
  • 138
599
votes
15 answers

How to read a large file line by line?

I want to read a file line by line, but without completely loading it in memory. My file is too large to open in memory, and if try to do so I always get out of memory errors. The file size is 1 GB.
adnan masood
  • 6,015
  • 2
  • 13
  • 6
598
votes
12 answers

htmlentities() vs. htmlspecialchars()

What are the differences between htmlspecialchars() and htmlentities(). When should I use one or the other?
Eric Hogue
  • 8,800
  • 4
  • 26
  • 21