PHP Exercises, Practice, Solution
What is PHP?
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.
The best way we learn anything is by practice and exercise questions. We have started this section for those (beginner to intermediate) who are familiar with PHP.
Hope, these exercises help you to improve your PHP coding skills. Currently, following sections are available, we are working hard to add more exercises. Happy Coding!
Note: It's fine if you are playing around with PHP codes with the help of an online PHP editor, to enjoy a full-fledged PHP environment (since online editors have several caveats, e.g. embedding PHP within HTML) up and running on your own machine is much better of an option to learn PHP. Please read our installing PHP on Windows and Linux if you are unfamiliar to PHP installation.
List of PHP Exercises :
- PHP Basic : 102 Exercises with Solution
- PHP Basic Algorithm: 136 Exercises with Solution
- PHP arrays : 59 Exercises with Solution
- PHP for loop : 38 Exercises with Solution
- PHP functions : 6 Exercises with Solution
- PHP classes : 7 Exercises with Solution
- PHP Regular Expression : 7 Exercises with Solution
- PHP Date : 28 Exercises with Solution
- PHP String : 26 Exercises with Solution
- PHP Math : 12 Exercises with Solution
- PHP JSON : 4 Exercises with Solution
- PHP Searching and Sorting Algorithm : 17 Exercises with Solution
- More to Come !
PHP Challenges :
- PHP Challenges: Part -1 [ 1- 25]
- More to come
Note : You may accomplish the same task (solution of the exercises) in various ways, therefore the ways described here are not the only ways to do stuff. Rather, it would be great, if this helps you anyway to choose your own methods.
Popularity of Programming Language Worldwide, Jun 2022 compared to a year ago:
Rank | Change | Language | Share | Trend |
---|---|---|---|---|
1 | Python | 27.61 % | -2.8 % | |
2 | Java | 17.64 % | -0.7 % | |
3 | Javascript | 9.21 % | +0.4 % | |
4 | C# | 7.79 % | +0.8% | |
5 | C/C++ | 7.01 % | +0.4 % | |
6 | PHP | 5.27 % | -1.0 % | |
7 | R | 4.26 % | +0.5 % | |
8 | ![]() |
TypeScript | 2.43 % | +0.7 % |
9 | ![]() |
Objective-C | 2.21 % | +0.1 % |
10 | ![]() |
Swift | 2.17 % | +0.4 % |
11 | ![]() |
Matlab | `1.71% | +0.2 % |
12 | ![]() |
Kotlin | 1.57 % | -0.2 % |
13 | Go | 1.48 % | +0.0 % | |
14 | ![]() |
Rust | 1.29 % | +0.4 % |
15 | Ruby | 1.1 % | -0.0% | |
16 | ![]() |
VBA | 1.07 % | -0.2 % |
17 | ![]() |
Ada | 0.95 % | +0.4 % |
18 | ![]() |
Scala | 0.73 % | +0.2 % |
19 | ![]() |
Visual Basic | 0.65 % | -0.0 % |
20 | ![]() |
Dart | 0.64 % | +0.0 % |
21 | ![]() |
Abap | 0.58 % | +0.1 % |
22 | ![]() |
Lua | 0.51 % | -0.0 % |
23 | ![]() |
Groovy | 0.48 % | +0.1 % |
24 | ![]() |
Perl | 0.44 % | +0.0 % |
25 | Julia | 0.41 % | +0.0 % | |
26 | Cobol | 0.34 % | +0.1 % | |
27 | Haskell | 0.29 % | +0.1 % | |
28 | Delphi/Pascal | 0.16 % | +0.1 % |
Source : https://pypl.github.io/PYPL.html
TIOBE Index for June 2022
June 2022 | June 2021 | Change | Programming Language | Ratings | Change |
---|---|---|---|---|---|
1 | 2 | ![]() |
Python | 12.20% | +0.35% |
2 | 1 | ![]() |
C | 11.91% | -0.64% |
3 | 3 | Java | 10.47% | -1.07% | |
4 | 4 | C++ | 9.63% | +2.26% | |
5 | 5 | C# | 6.12% | +1.79% | |
6 | 6 | Visual Basic | 5.42% | +1.40% | |
7 | 7 | JavaScript | 2.09% | -0.24% | |
8 | 10 | ![]() |
SQL | 1.94% | +0.06% |
9 | 9 | Assembly language | 1.85% | -0.21% | |
10 | 16 | ![]() |
Swift | 1.55% | +0.44% |
11 | 11 | Classic Visual Basic | 1.33% | -0.40% | |
12 | 18 | ![]() |
Delphi/Object Pascal | 1.32% | +0.26% |
13 | 8 | ![]() |
PHP | 1.25% | -0.97% |
14 | 23 | ![]() |
Objective-C | 1.02% | +0.33% |
15 | 20 | ![]() |
Go | 1.02% | +0.07% |
16 | 14 | ![]() |
R | 0.98% | -0.22% |
17 | 15 | ![]() |
Perl | 0.76% | -0.41% |
18 | 38 | ![]() |
Lua | 0.76% | +0.43% |
19 | 13 | ![]() |
Ruby | 0.75% | -0.48% |
20 | 26 | ![]() |
Prolog | 0.74% | +0.18% |
Source : https://www.tiobe.com/tiobe-index/
[ Want to contribute to PHP exercises? Send your code (attached with a .zip file) to us at w3resource[at]yahoo[dot]com. Please avoid copyrighted materials.]
List of Exercises with Solutions :
- HTML CSS Exercises, Practice, Solution
- JavaScript Exercises, Practice, Solution
- jQuery Exercises, Practice, Solution
- jQuery-UI Exercises, Practice, Solution
- CoffeeScript Exercises, Practice, Solution
- Twitter Bootstrap Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution
- C# Sharp Programming Exercises, Practice, Solution
- PHP Exercises, Practice, Solution
- Python Exercises, Practice, Solution
- Java Exercises, Practice, Solution
- SQL Exercises, Practice, Solution
- MySQL Exercises, Practice, Solution
- PostgreSQL Exercises, Practice, Solution
- SQLite Exercises, Practice, Solution
- MongoDB Exercises, Practice, Solution
PHP: Tips of the Day
The ternary operator can be thought of as an inline if statement. It consists of three parts. The operator, and two outcomes. The syntax is as follows:
Example:
$value = <operator> ? <true value> : <false value>
If the operator is evaluated as true, the value in the first block will be returned (<true value>), else the value in the second block will be returned (<false value>). Since we are setting $value to the result of our ternary operator it will store the returned value.
Example:
$action = empty($_POST['action']) ? 'default' : $_POST['action'];
$action would contain the string 'default' if empty($_POST['action']) evaluates to true. Otherwise it would contain the value of $_POST['action'].
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1evaluates to true, and expr3 if expr1 evaluates to false.
It is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise. ?: is often referred to as Elvis operator.
This behaves like the Null Coalescing operator ??, except that ?? requires the left operand to be exactly null while ?: tries to resolve the left operand into a boolean and check if it resolves to boolean false.
Example:
function setWidth(int $width = 0){ $_SESSION["width"] = $width ?: getDefaultWidth(); }
In this example, setWidth accepts a width parameter, or default 0, to change the width session value. If $width is 0 (if $width is not provided), which will resolve to boolean false, the value of getDefaultWidth() is used instead. The getDefaultWidth() function will not be called if $width did not resolve to boolean false.
Refer to Types for more information about conversion of variables to boolean.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework