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
11 answers

How do I get the HTML code of a web page in PHP?

I want to retrieve the HTML code of a link (web page) in PHP. For example, if the link is https://stackoverflow.com/questions/ask then I want the HTML code of the page which is served. I want to retrieve this HTML code and store it in a PHP…
djmzfKnm
  • 26,679
  • 70
  • 166
  • 227
111
votes
22 answers

PHP: How to check if image file exists?

I need to see if a specific image exists on my cdn. I've tried the following and it doesn't work: if (file_exists(http://www.example.com/images/$filename)) { echo "The file exists"; } else { echo "The file does not exist"; } Even if the…
PaperChase
  • 1,557
  • 4
  • 18
  • 23
111
votes
16 answers

PHP file_get_contents() returns "failed to open stream: HTTP request failed!"

I am having problems calling a url from PHP code. I need to call a service using a query string from my PHP code. If I type the url into a browser, it works ok, but if I use file-get-contents() to make the call, I get: Warning:…
undefined
  • 5,190
  • 11
  • 56
  • 90
111
votes
3 answers

Send JSON POST request with PHP

I have this JSON data: { userID: 'a7664093-502e-4d2b-bf30-25a2b26d6021', itemKind: 0, value: 1, description: 'Saude', itemID: '03e76d0a-8bab-11e0-8250-000c29b481aa' } and I need to post to JSON…
CMartins
  • 3,247
  • 5
  • 38
  • 53
111
votes
9 answers

Page redirect after certain time PHP

There is a certain PHP function for redirecting after some time. I saw it somewhere but can't remember. It's like the gmail redirection after logging in. Please, could anyone remind me?
afaolek
  • 8,452
  • 13
  • 45
  • 60
111
votes
2 answers

Pros and Cons of Interface constants

PHP interfaces allow the definition of constants in an interface, e.g. interface FooBar { const FOO = 1; const BAR = 2; } echo FooBar::FOO; // 1 Any implementing class will automatically have these constants available, e.g. class MyFooBar…
Gordon
  • 312,688
  • 75
  • 539
  • 559
111
votes
4 answers

How does "do something OR DIE()" work in PHP?

I'm writing a php app to access a MySQL database, and on a tutorial, it says something of the form mysql_connect($host, $user, $pass) or die("could not connect"); How does PHP know that the function failed so that it runs the die part? I guess I'm…
chustar
  • 12,225
  • 24
  • 81
  • 119
111
votes
12 answers

What is the best practice for adding constants in laravel? (Long List)

I am rather new to laravel. I have a basic question, What is the best way to add constants in laravel. I know the .env method that we use to add the constants. Also I have made one constants file to use them for my project. For…
Faran Khan
  • 1,495
  • 4
  • 14
  • 26
111
votes
8 answers

Is there a point to minifying PHP?

I know you can minify PHP, but I'm wondering if there is any point. PHP is an interpreted language so will run a little slower than a compiled language. My question is: would clients see a visible speed improvement in page loads and such if I were…
Bojangles
  • 99,427
  • 50
  • 170
  • 208
111
votes
6 answers

Manually register a user in Laravel

Is it possible to manually register a user (with artisan?) rather than via the auth registration page? I only need a handful of user accounts and wondered if there's a way to create these without having to set up the registration controllers and…
Matt Ellis
  • 1,303
  • 2
  • 9
  • 7
111
votes
6 answers

Laravel 5.2 - Use a String as a Custom Primary Key for Eloquent Table becomes 0

I am trying to use email as my table's primary key, so my eloquent code is-
Abrar Jahin
  • 13,970
  • 24
  • 112
  • 161
111
votes
9 answers

Eloquent ORM laravel 5 Get Array of ids

I'm using Eloquent ORM laravel 5.1, and I want to return an array of ids greater than 0, my model is called test. I have tried : $test=test::select('id')->where('id' ,'>' ,0)->get()->toarray(); It returns : Array ( [0] => Array ( [id] => 1 ) [1] =>…
paranoid
  • 6,799
  • 19
  • 49
  • 86
111
votes
9 answers

Can I do Model->where('id', ARRAY) multiple where conditions?

The title says it all. I get that I can do this : DB::table('items')->where('something', 'value')->get() But what I want to check the where condition for multiple values like so: DB::table('items')->where('something', 'array_of_value')->get() Is…
Vudew
  • 1,235
  • 2
  • 9
  • 8
111
votes
30 answers

PHPUnit - 'No tests executed' when using configuration file

The Problem To improve my quality of code, I've decided to try to learn how to test my code using Unit Testing instead of my mediocre-at-best testing solutions. I decided to install PHPUnit using composer for a personal library that allows me to…
user4165455
111
votes
16 answers

Why is PHP Composer so slow?

Why is PHP Composer so slow when all I do is init a project with zero dependencies? Here are the commands I run: composer init composer install Wait 3 minutes (not an…
AgmLauncher
  • 7,070
  • 8
  • 41
  • 67