w3resource

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 Challenges :

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, Oct 2020 compared to a year ago:

Rank Change Language Share Trend
1 Python 31.02 % +2.2 %
2 Java 16.38 % -2.8 %
3 Javascript 8.41 % +0.4 %
4 C# 6.52 % -0.6%
5 PHP 5.83 % -0.4 %
6 C/C++ 5.56 % -0.4 %
7 R 4.26 % +0.4 %
8 Objective-C 3.48 % +0.8 %
9 Swift 2.37 % -0.1 %
10 TypeScript 1.9 % +0.1 %
11 Matlab 1.67 % -0.1 %
12   Kotlin 1.66 % +0.1 %
13 up arrow Go 1.43 % +0.2 %
14 down arrow Ruby 1.17 % -0.3 %
15 down arrow VBA 1.12 % -0.2 %
16 up arrow Rust 1.01 % +0.4 %
17 down arrow Scala 0.95 % -0.1 %
18 down arrow Visual Basic 0.78 % -0.2 %
19 up arrow Dart 0.61 % +0.2 %
20 up arrow Ada 0.59 % +0.2 %
21 up arrow Lua 0.52 % +0.2 %
22 down arrow Perl 0.48 % -0.1 %
23 down arrow Groovy 0.45 % -0.1 %
24 up arrow Julia 0.43 % +0.2 %
25 down arrow Abap 0.41 % -0.1 %
26 down arrow Cobol 0.36 % +0.0 %
27 up arrow Haskell 0.33 % +0.0 %
28 Delphi 0.31 % +0.1 %

Source : http://pypl.github.io/PYPL.html

TIOBE Index for October 2020

Oct 2020 Oct 2019 Change Programming Language Ratings Change
1 2 up arrow C 16.95% +0.77%
2 1 down arrow Java 12.56% -4.32%
3 3 Python 11.28% +2.19%
4 4 C++ 6.94% +0.71%
5 5 C# 4.16% +0.30%
6 6 Visual Basic 3.97% +0.23%
7 7 JavaScript 2.14% +0.06%
8 9 up arrow PHP 2.09% +0.18%
9 15 up arrow R 1.99% +0.73%
10 8 down arrow SQL 1.57% -0.37%
11 19 up arrow Perl 1.43% +0.40%
12 11 up arrow Groovy 1.23% -0.16%
13 13 up arrow Ruby 1.16% -0.16%
14 17 down arrow Go 1.16% +0.06%
15 20 up arrow MATLAB 1.12% +0.19%
16 12 down arrow Swift 1.09% -0.28%
17 14 down arrow Assembly language 1.08% -0.23%
18 10 down arrow Objective-C 0.86% -0.64%
19 16 down arrow Classic Visual Basic 0.77% -0.46%
20 22 up arrow PL/SQL 0.77% -0.06%

Source : https://www.tiobe.com/tiobe-index/

List of Exercises with Solutions :

[ 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 :



PHP: Tips of the Day

PHP: Using str_replace so that it only acts on the first match?

Can be done with preg_replace:

function str_replace_first($from, $to, $content)
{
    $from = '/'.preg_quote($from, '/').'/';

    return preg_replace($from, $to, $content, 1);
}

echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); 
// outputs '123def abcdef abcdef'

The magic is in the optional fourth parameter [Limit]. From the documentation:

[Limit] - The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

Ref : https://bit.ly/35SCuP8