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

Curl error 60, SSL certificate issue: self signed certificate in certificate chain

I try to send curl request with my correct APP_ID, APP_SECRET etc. to the …
Victor Bocharsky
  • 11,930
  • 13
  • 58
  • 91
109
votes
20 answers

open_basedir restriction in effect. File(/) is not within the allowed path(s):

I'm getting this error on an avatar upload on my site. I've never gotten it before and nothing was changed recently for me to begin getting this error... Warning: is_writable() [function.is-writable]: open_basedir restriction in effect. File(/)…
Ben
  • 60,438
  • 111
  • 314
  • 488
109
votes
7 answers

Can I Install Laravel without using Composer?

I'd like to know if I can install or use the Laravel PHP framework on any web server without using Composer (PHP package/dependency manager) every time? I would like to be able to drop my app on to any web server (like a shared server without access…
ryanwinchester
  • 11,737
  • 5
  • 27
  • 45
109
votes
9 answers

AngularJS HTTP post to PHP and undefined

I have a form with the tag ng-submit="login() The function gets called fine in javascript. function LoginForm($scope, $http) { $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; $scope.email …
Ronnie
  • 11,138
  • 21
  • 78
  • 140
109
votes
3 answers

composer.lock: how does it work?

I'm trying to understand this part: http://getcomposer.org/doc/02-libraries.md#lock-file this lock file will not have any effect on other projects that depend on it. It only has an effect on the main project" Does that mean that if project P…
HappyDeveloper
  • 12,480
  • 22
  • 82
  • 117
108
votes
5 answers

How to post JSON to PHP with curl

I may be way off base, but I've been trying all afternoon to run the curl post command in this recess PHP framework tutorial. What I don't understand is how is PHP supposed to interpret my POST, it always comes up as an empty array. curl -i -X…
Peter Turner
  • 11,199
  • 10
  • 68
  • 109
108
votes
9 answers

'xmlParseEntityRef: no name' warnings while loading xml into a php file

I am reading an xml in php using simplexml_load_file. However while trying to load the xml it displays a list of warnings Warning: simplexml_load_file() [function.simplexml-load-file]:
Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294
108
votes
16 answers

Using an array as needles in strpos

How do you use the strpos for an array of needles when searching a string? For example: $find_letters = array('a', 'c', 'd'); $string = 'abcdefg'; if(strpos($string, $find_letters) !== false) { echo 'All the letters are found in the…
MacMac
  • 34,294
  • 55
  • 151
  • 222
108
votes
10 answers

MySQL and PHP - insert NULL rather than empty string

I have a MySQL statement that inserts some variables into the database. I recently added 2 fields which are optional ($intLat, $intLng). Right now, if these values are not entered I pass along an empty string as a value. How do I pass an explicit…
user547794
  • 14,263
  • 36
  • 103
  • 152
108
votes
5 answers

Installing GD in Docker

I am a complete Docker novice but am having to maintain an existing system. The Dockerfile I am using is as below: FROM php:5.6-apache RUN docker-php-ext-install mysql mysqli RUN apt-get update -y && apt-get install -y sendmail RUN apt-get update…
evilscary
  • 2,097
  • 5
  • 23
  • 33
108
votes
9 answers

Reconnection of Client when server reboots in WebSocket

I am using web socket using PHP5 and the Chrome browser as client. I have taken the code from the site http://code.google.com/p/phpwebsocket/. I run the server, and the client is also connected. I can chat as well. Now when I restart the server (by…
siddhusingh
  • 1,832
  • 4
  • 25
  • 30
108
votes
19 answers

How to force Laravel Project to use HTTPS for all routes?

I am working on a project that requires a secure connection. I can set the route, uri, asset to use 'https' via: Route::get('order/details/{id}', ['uses' => 'OrderController@details', 'as' => 'order.details', 'https']); url($language.'/index', [],…
Nelson Melecio
  • 1,334
  • 3
  • 12
  • 19
108
votes
14 answers

Get environment value in controller

In my .env file I have the following: IMAP_HOSTNAME_TEST=imap.gmail.com IMAP_USERNAME_TEST=myemail@gmail.com IMAP_PASSWORD_TEST=mypw Now I would like to use them in my controller. I've tried this, but without any result: $hostname =…
nielsv
  • 6,540
  • 35
  • 111
  • 215
108
votes
8 answers

Sort multidimensional array by multiple columns

I'm trying to sort a multidimensional array by multiple keys, and I have no idea where to start. I looked at uasort(), but wasn't quite sure how to write a function for what I need. I need to sort by the state, then event_type, then date_start. My…
attepted_nerd
  • 1,083
  • 2
  • 8
  • 5
108
votes
5 answers

How to do error logging in CodeIgniter (PHP)

I want error logging in PHP CodeIgniter. How do I enable error logging? I have some questions: What are all the steps to log an error? How is an error log file created? How to push the error message into log file (whenever an error occurs)? How…
udaya
  • 9,598
  • 15
  • 48
  • 67