The Wayback Machine - https://web.archive.org/web/20240926143306/https://www.geeksforgeeks.org/php-examples/
Open In App

PHP Programs

Last Updated : 29 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

PHP Programs is a collection of coding examples and practical exercises designed to help beginners and experienced developers. This collection covers a wide range of questions based on Array, Stirng, Date, Files, …, etc. Each programming example includes multiple approaches to solve the problem.

Whether you are preparing for coding interviews, working on web development projects, or simply looking to improve your PHP skills, these programs offer clear explanations, step-by-step solutions, and best practices to enhance your understanding and proficiency in PHP programming.

PHP Programs

PHP Basic Programs

Explore essential PHP programs covering fundamental concepts and syntax examples.

1 How to write PHP code in different ways?
2 How to write comments in PHP?
3 How to use Codeignitor (PHP)?
4 How to echo HTML in PHP?
5 How to do Error handling in PHP?
6 How to show All Errors in PHP?
7 How to Start and Stop a Timer in PHP?
8 How to create default function parameter in PHP?
9 How to check if mod_rewrite is enabled in PHP?
10 How to do Web Scrapping in PHP Using Simple HTML DOM Parser?
11 How to pass form variables from one page to another page in PHP?
12 How to display logged in user information in PHP?
13 How to find out where a function is defined using PHP?
14 How to Get $_POST from multiple check-boxes?
15 How to Secure hash and salt for PHP passwords?
16 How to detect search engine bots with PHP?
17 How to set PHP development environment in windows?
18 How to turn off PHP Notices?
19 How to use ‘<?=’ short open tag in PHP programming?

PHP Array Programs

Enhance your PHP skills with array-focused programs covering key concepts and functionalities.

1 How to insert a new item in an array on any position in PHP?
2 How to append one array to another in PHP?
3 How to delete an Element From an Array in PHP?
4 How to print all the values of an array in PHP?
5 How to perform Array Delete by Value Not Key in PHP?
6 How to remove Array Element and do Re-Indexing in PHP?
7 How to count all array elements in PHP?
8 How to insert an item at the beginning of an array in PHP?
9 How to check if two arrays contain the same elements?
10 How to merge two arrays keeping original keys in PHP?
11 How to find the maximum and the minimum in a PHP array?
12 How to check a key exists in an array in PHP?
13 How to find the second most frequent element in a PHP array?
14 How to sort an array of objects by object fields in PHP?
15 How to sort an array of strings in natural and standard orders?
16 How to print the last value of an array without affecting the pointer?
17 How to merge the first index of an array with the first index of the second array?
18 How to create a string by joining the array elements using PHP?
19 How to sort an Array of Associative Arrays by Value of a Given Key in PHP?
20 How to make a leaderboard using PHP?
21 How to check an array is multidimensional or not in PHP?
22 How to create Multidimensional Associative Array in PHP?
23 How to merge the duplicate value in multidimensional array in PHP?
24 How to convert multidimensional array to XML file in PHP?
25 How to search by multiple key => value in PHP array?
26 How to search by key=>value in a multidimensional array in PHP?
27 How to write a PHP program to find the Standard Deviation of an array?
28 How to write PHP program to check for Anagram?

PHP Function Programs

Explore PHP functions through various programs, covering fundamental concepts and advanced functionalities for comprehensive understanding and practice.

1 How to pass PHP Variables by reference?
2 How to format Phone Numbers in PHP?
3 How to use php serialize() and unserialize() Function
4 How to implement callback in PHP?
5 How to merge two or more arrays using array_merge()?
6 How to print an arithmetic progression series using inbuilt functions in PHP?
7 How to prevent SQL Injection in PHP?
8 How to extract the user name from the email ID using PHP?
9 How to count rows in MySQL table in PHP?
10 How to parse a CSV File in PHP?
11 How to generate simple random password from a given string using PHP?
12 How to upload images in MySQL using PHP PDO?
13 How to check foreach Loop Key Value in PHP?
14 How to properly Format a Number With Leading Zeros in PHP?
15 How to get a File Extension in PHP?
16 How to build a Grocery Store Web App using PHP with MySQL?
17 How to delete text from file using preg_replace() function in PHP?

PHP Date Programs

Enhance PHP expertise with date programs demonstrating manipulation, formatting, and processing of dates, catering to various scenarios and requirements.

1 How to get the current Date and Time in PHP?
2 How to change the date format using PHP?
3 How to convert DateTime to String using PHP?
4 How to get Time Difference in Minutes in PHP?
5 How to return all dates between two dates in an array in PHP?
6 How to sort an array of dates in PHP?
7 How to get the time of the last modification of the current page in PHP?
8 How to convert a Date into Timestamp using PHP?
9 How to add 24 hours to a unix timestamp in PHP?
10 How to sort a multidimensional array by date element in PHP?
11 How to convert timestamp to readable date/time in PHP?
12 How to find number of week days between two dates?
13 How to convert string to Date and DateTime in PHP?
14 How to get last day of a month from date in PHP?

PHP String Programs

Learn PHP String Programs, covering a wide range of string operations such as manipulation, searching, and formatting for diverse applications.

1 How to change strings in an array to uppercase?
2 How to convert first character of all the words uppercase using PHP?
3 How to get the last character of a string in PHP?
4 How to convert uppercase string to lowercase using PHP?
5 How to extract Numbers From a String in PHP?
6 How to replace String in PHP?
7 How to Encrypt and Decrypt a PHP String?
8 How to display string values within a table using PHP?
9 How to write Multi-Line Strings in PHP?
10 How to check if a String Contains a Substring in PHP?
11 How to append a string in PHP?
12 How to remove white spaces only beginning/end of a string using PHP?
13 How to Remove Special Character from String in PHP?
14 How to create a string by joining the array elements using PHP?
15 How to prepend a string in PHP?
16 How to replace a word inside a string in PHP?
17 How to remove all white spaces from a string in PHP?
18 How to count the number of words in a string in PHP?
19 How to find number of characters in a string in PHP?
20 How to get a substring between two strings in PHP?
21 How to get a variable name as a string in PHP?
22 How to remove occurrences of a specific character from end of a string in PHP?
23 How to convert string to boolean in PHP?
24 How to generate Random String Using PHP?
25 How to generate a random, unique, alphanumeric string in PHP?
26 How to remove new lines from string in PHP?
27 How to insert string at specified position in PHP?
28 How to check a string is a rotation of another string?

PHP Classes Programs

Learn PHP Classes Programs, delving into object-oriented programming concepts like inheritance, encapsulation, and polymorphism for robust application development.

1 How to use PHP Access Specifiers?
2 How to use PHP Constructors and Destructors?
3 How to do Type Casting?
4 How to convert of an Object to an Object of other class?
5 How to merge two PHP objects?
6 How to use Abstract Classes in PHP?

PHP JSON Programs

Learn PHP JSON Programs to manipulate JSON data, parse JSON strings, and encode PHP arrays into JSON format for efficient data exchange.

1 How to parse a JSON File in PHP?
2 How to generate JSON File in PHP?
3 How to Convert JSON file into CSV in PHP?
4 How to Convert XML data into JSON using PHP?
5 How to Insert JSON data into MySQL database using PHP?
6 How to convert PHP array to JavaScript or JSON?
7 How to receive JSON POST with PHP?
8 How to use cURL to Get JSON Data and Decode JSON Data in PHP?

PHP File Systems Programs

Learn PHP File Systems Programs to perform file operations such as reading, writing, deleting, and manipulating files and directories efficiently.

1 How to Create a Folder if It Doesn’t Exist in PHP?
2 How to check if File Exists in PHP?
3 How to write Into a File in PHP?
4 How to delete all files from a folder using PHP?
5 How to get file name from a path in PHP?
6 How to log errors and warnings into a file in PHP?
7 How to extract extension from a filename using PHP?
8 How to get names of all the subfolders and files present in a directory using PHP?


Similar Reads

Output of PHP programs | Set 1 (Regular Expressions)
Predict the output of following PHP programs: Question 1 <?php echo str_pad("Welcome", 5)." to GeeksforGeeks."; ?> Options: WelcomeWelcomeWelcomeWelcomeWelcome to GeeksforGeeks. to GeeksforGeeks. WelcomeWelcomeWelcomeWelcomeWelcome to GeeksforGeeks. Welcome Welcome to GeeksforGeeks. Output: Welcome to GeeksforGeeks. Explan
3 min read
Output of PHP programs | Set 2 ( Filters )
Predict the output of the following PHP programs: Question 1 <?php $num = "123"; if (!filter_var($num, FILTER_VALIDATE_INT)) echo("Hello"); else echo("Welcome to GeeksforGeeks"); ?> Options: No output is returned Hello Welcome to GeeksforGeeks Error Output: Welcome to GeeksforGeeks Explanation: filter_var() – Fil
2 min read
Output of PHP programs | Set 3
Predict the output of below PHP programs: Question 1 <?php $number = array(0, 1, one, two, three, 5); $num = preg_grep("/[0-5]/", $number); print_r($num); ?> Options: Array([0]=>0 [1]=>1 [2]=>one [3]=>two [4]=>three [5]=>5) Array([2]=>one [3]=>two [4]=>three) Array([1]=> 1) Array([0]=>0 [1]=>1 [5]
3 min read
Output of PHP programs | Set 4
Predict the output of below PHP programs: Question 1 <?php "Welcome to GeeksforGeeks!" ?> Options: Error Welcome to GeeksforGeeks! Nothing Missing semicolon error Output: Nothing Explanation: If you need to output something onto the screen you’ll need to use echo or print_r. Question 2 <?php print_r "I am intern at GeeksforG
2 min read
PHP programs for printing pyramid patterns
This article is aimed at giving a PHP implementation for pattern printing. Simple Pyramid Pattern C/C++ Code <?php // Php code to demonstrate // star pattern // Function to demonstrate // printing pattern function pypart($n) { // Outer loop to handle number // of rows in this case for ($i = 0; $i < $n; $i++) { // inner loop to handle // numbe
6 min read
How to run PHP programs ?
XAMPP is a Web development tool, created by Apache, that makes it easy to run PHP (Personal Home Pages) scripts on your computer locally. Installation of XAMPP Server on windows is easy as compared to manual installation of a web server and PHP required a lot of in-depth configuration knowledge. XAMPP package installs MySQL, FileZilla, Mercury, Per
3 min read
PHP Array Programs
PHP Arrays is a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data. S. No Articles 1PHP Program to Insert a New Element in an Array2PHP Program to Find the Index of an Element in an Array3PHP Program to Display Arr
4 min read
PHP String Programs
Strings can be seen as a stream of characters. For example, ‘G’ is a character, and ‘GeeksforGeeks’ is a string. This article contains a list of all programming articles based on String in PHP that are majorly asked in Interviews. S. No. Articles 1PHP Program to Read Each Character of a String 2PHP Program to Check for Substring in a String3PHP Pro
3 min read
Output of JavaScript Programs
In this article, we will see the outputs of various Javascript programs. Predict and Explain the Output of the below JavaScript programs. Example 1: When (x, y, z) is logged, x gives value 4 (as primitives are passed by value, and hence its value does not change even after function f()). y is an array, hence an object, and so it is passed by refere
3 min read
JavaScript Linked List Programs
JavaScript Linked List Programs contain a list of articles based on programming. Linked List is a linear data structure that stores data in linearly connected nodes. Linked lists store elements sequentially, but doesn’t store the elements contiguously like an array. S. NoArticles1JavaScript Program to Implement Linked List2Javascript Program to Sea
5 min read
JavaScript Programs
JavaScript Programs contains list of articles based on programming. This article contains wide collection of programming articles based on Numbers, Maths, Arrays, Strings, ..., etc. that are mostly asked in interviews. Table of Content Basic Programs Number Programs Math Programs Array Programs String Programs Linked List Programs Object Programs D
15+ min read
How to write programs in your website and redirect it in JDoodle online compiler ?
There may be situations where it may be required to run a piece of code that has been entered by the user. This can be done by using JDoodle. We can write our programs in any programming language on our website page and it can be redirected to the JDoodle online compiler to be run on that website. Note: It is not mandatory to sign in or register in
2 min read
PHP 5 vs PHP 7
PHP is a server side scripting language designed for web development by Rasmus Lerdorf in 1994. Since its launch in 1994 PHP has become an industry standard supporting almost 80% of the websites ( 79.8% to be precise) with its closest competitor being ASP.Net at 19.8% and others like Ruby, Java trailing far behind. The PHP development team released
4 min read
How to include content of a PHP file into another PHP file ?
Including the content of a PHP file into another file reduces the complexity of code by reducing the code and improving its modularity by splitting the code into different files so that it is easy to understand and manage. There are two ways to do it by using the following PHP functions. PHP include() Function: It will show the warning if the file
2 min read
What is New in PHP Type Hinting Support in PHP 8?
PHP 8 introduced several enhancements and new features related to type hinting. That will check the code for their respective type so that we can use our code efficiently. These are the following features used in PHP Type Hitting Support: Table of Content Union TypesMixed TypeStatic Return TypeInheritance of Contravariant ParametersInheritance of P
3 min read
PHP php.ini File Configuration
At the time of PHP installation, php.ini is a special file provided as a default configuration file. It's a very essential configuration file that controls, what a user can or cannot do with the website. Each time PHP is initialized, the php.ini file is read by the system. Sometimes you need to change the behavior of PHP at runtime, then this confi
5 min read
How to Import config.php File in a PHP Script?
The include() function in PHP copies the code from the specified file into the file that uses the include statement. It directs the preprocessor to insert the content of the specified file into the current program. The name of the file to be included is written in double quotes. It is good practice to write basic database and user details in a file
2 min read
LAMP installation and important PHP configurations on Ubuntu
PHP is the Web development (Server Side Scripting Language) written and developed for web developers.It was developed by Rasmus Lerdorf in 1994, for maintaining his personal homepage. Later he added the feature to communicate with web forms and to connect with databases, named it as Personal Home Page Tools (PHP), Lerdorf wrote those using C binari
5 min read
PHP | imagecreatetruecolor() Function
The imagecreatetruecolor() function is an inbuilt function in PHP which is used to create a new true-color image. This function returns a blank image of the given size. Syntax: resource imagecreatetruecolor( $width, $height ) Parameters: This function accepts two parameters as mentioned above and described below: $width: This parameter is used to s
2 min read
PHP fpassthru( ) Function
The fpassthru() function in PHP is an inbuilt function which is used to read data from a current position from a specified file until end of file and then write the result to the output buffer. The file which has to be read is sent as a parameter to the fpassthru() function and it returns the number of characters passed on success or FALSE on failu
2 min read
Maximum execution time taken by a PHP Script
One important aspect of PHP programs is that the maximum time taken to execute a script is 30 seconds. The time limit varies depending on the hosting companies but the maximum execution time is between 30 to 60 seconds. The user may get errors of maximum time limit exceeds due to some heavy import or export of files or program which involves sendin
4 min read
PHP | ImagickDraw getTextAlignment() Function
The ImagickDraw::getTextAlignment() function is an inbuilt function in PHP which is used to get the alignment applied when annotating with text. It helps to format the text by making it stick to left, right or middle. Syntax: int ImagickDraw::getTextAlignment( void ) Parameters: This function doesn’t accept any parameter. Return Value: This functio
2 min read
PHP | Ds\Sequence last() Function
The Ds\Sequence::last() function is an inbuilt function in PHP which is used to return the last element from the sequence. Syntax: mixed abstract public Ds\Sequence::last( void ) Parameters: This function does not accept any parameters. Return value: This function returns the last element from the sequence. Below programs illustrate the Ds\Sequence
1 min read
PHP | Imagick floodFillPaintImage() Function
The Imagick floodFillPaintImage() Function is an in-built function in PHP which is used to change the color value of any pixel that matches the target, along with its immediate neighbor pixels of the same color. Note: This method is a replacement for deprecated Imagick::paintFloodFillImage() Function and is available if Imagick has been compiled ag
4 min read
Function to escape regex patterns before applied in PHP
Use preg_quote() function in PHP to escape regex patterns before it is applied in run time. The preg_quote() function puts a backslash in front of every character within the specified string that would be a part of the regex syntax in PHP, thereby making them escape sequences. These characters include: . \ + * ? [ ^ ] $ ( ) { } = ! | : - # Syntax:
3 min read
PHP | array_udiff_uassoc() Function
The array_udiff_uassoc() Function is an inbuilt function in PHP and is used for distinguishing between two or more array. The function computes the differences array by use of two user-defined functions with the additional index key. It compares both data and index by use of callback functions and returns the difference. Syntax : array_udiff_uassoc
5 min read
How to generate Json File in PHP ?
In this article, we are going to generate a JSON file in PHP by using an array. JSON stands for JavaScript object notation, which is used for storing and exchanging data. JSON is text, written with JavaScript object notation. Structure: {"data":[ { "sub_data1":"value1", "sub_data2":"value2","sub_data_n":"value n" }, { "sub_data2":"value2","sub_data
3 min read
Difference and Similarities between PHP and C
PHP is a server-side scripting language designed specifically for web development. It can be easily embedded in HTML files and HTML codes can also be written in a PHP file. The thing that differentiates PHP from a client-side language like HTML is, PHP codes are executed on the server whereas HTML codes are directly rendered on the browser. C is a
3 min read
PHP | geoip_continent_code_by_name() Function
The geoip_continent_code_by_name() function is an inbuilt function in PHP which helps to generate a two-letter continent code(Each continent has assigned a two-letter code. For example - AF for Africa, AS for Asia, and many more). The function takes the hostname or IP Address as an argument and generates the two-letter continent code. Syntax : stri
1 min read
How to secure database passwords in PHP?
Most of the websites are providing sing up and login facility to the user. User has to create a password and use it for login to the website. But it is very important to secure the password of the user. password_hash() function provides the facility to securely store the password of the user to the database. Syntax password_hash(Password, PASSWORD_
2 min read