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
107
votes
16 answers

json_decode to custom class

Is it possible to decode a json string to an object other than stdClass?
Tom
  • 1,191
  • 2
  • 8
  • 7
107
votes
8 answers

Checking if date is weekend PHP

This function seems to only return false. Are any of you getting the same? I'm sure I'm overlooking something, however, fresh eyes and all that ... function isweekend($date){ $date = strtotime($date); $date = date("l", $date); $date =…
JS1986
  • 1,920
  • 4
  • 29
  • 50
107
votes
3 answers

How to set the default value of an attribute on a Laravel model

How to set the default value of an attribute on a Laravel model? Should I set the default when creating a migration or should I set it in the model class?
Zeeshan Bin Iqbal
  • 1,206
  • 2
  • 8
  • 8
107
votes
11 answers

Check if value isset and null

I need to check if value is defined as anything, including null. isset treats null values as undefined and returns false. Take the following as an example: $foo = null; if(isset($foo)) // returns false if(isset($bar)) // returns…
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
107
votes
8 answers

Fastest Way to Serve a File Using PHP

I'm trying to put together a function that receives a file path, identifies what it is, sets the appropriate headers, and serves it just like Apache would. The reason I am doing this is because I need to use PHP to process some information about the…
Kirk Ouimet
  • 27,280
  • 43
  • 127
  • 177
107
votes
2 answers

PHP - remove all non-numeric characters from a string

What is the best way for me to do this? Should I use regex or is there another in-built PHP function I can use? For example, I'd want: 12 months to become 12. Every 6 months to become 6, 1M to become 1, etc.
b85411
  • 9,420
  • 15
  • 65
  • 119
107
votes
3 answers

When to generate a new Application Key in Laravel?

Since it automatically sets it for me in my .env file when I create the app, I'm not sure when I should run it. In addition to that, if a second developer comes in, and clones the app, does he/she need to run php artisan key:generate ? How do we…
code-8
  • 54,650
  • 106
  • 352
  • 604
107
votes
20 answers

PHP Warning: Module already loaded in Unknown on line 0

On Mac OSX Mavericks using homebrew php55 whenever I run a a php command I get the following error message (everything runs fine it's just annoying) PHP Warning: Module 'intl' already loaded in Unknown on line 0 I ran php --ini and the output…
Drew Landgrave
  • 1,515
  • 3
  • 13
  • 23
107
votes
11 answers

Convert/cast an stdClass object to another class

I'm using a third party storage system that only returns me stdClass objects no matter what I feed in for some obscure reason. So I'm curious to know if there is a way to cast/convert an stdClass object into a full fledged object of a given type.…
The Mighty Rubber Duck
  • 4,388
  • 5
  • 28
  • 27
107
votes
15 answers

array_unique for objects?

Is there any method like the array_unique for objects? I have a bunch of arrays with 'Role' objects that I merge, and then I want to take out the duplicates :)
user89862
107
votes
8 answers

Checking for an empty field with MySQL

I've wrote a query to check for users with certain criteria, one being they have an email address. Our site will allow a user to have or not have an email address. $aUsers=$this->readToArray(' SELECT `userID` FROM `users` WHERE `userID` …
user275074
107
votes
9 answers

Composer loading from cache

I ran into a problem using composer for installing/uninstalling some dependencies in laravel, which come back after deleting them from composer.json and deleting their vendor folder. I initially used dflydev's markdown package, but now I want to…
Tarik
  • 2,151
  • 4
  • 19
  • 26
107
votes
18 answers

Efficiently counting the number of lines of a text file. (200mb+)

I have just found out that my script gives me a fatal error: Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 440 bytes) in C:\process_txt.php on line 109 That line is this: $lines = count(file($path)) - 1; So I…
Abs
  • 56,052
  • 101
  • 275
  • 409
107
votes
12 answers

Download files in laravel using Response::download

In Laravel application I'm trying to achieve a button inside view that can allow user to download file without navigating to any other view or route Now I have two issues: (1) below function throwing The file "/public/download/info.pdf" does not…
DPP
  • 12,716
  • 3
  • 49
  • 46
107
votes
6 answers

How to check if a string contains a specific text

Suppose I have the code above, how to write the statement if($a exist 'some text')?
Lucas Fernandes
  • 1,360
  • 2
  • 8
  • 12
1 2 3
99
100