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, Sept 2021 compared to a year ago:

`
Rank Change Language Share Trend
1 Python 29.48 % -2.4 %
2 Java 17.18 % +0.7 %
3 Javascript 9.14 % +0.8 %
4 C# 6.94 % +0.6%
5 PHP 6.49 % +0.4 %
6 C/C++ 6.49 % +0.9 %
7 R 3.59 % -0.5 %
8 up arrow TypeScript 2.18 % +0.3 %
9 Swift 2.1 % -0.4 %
10 down arrow Objective-C 2.06 % -0.6 %
11 up arrow Kotlin1.91% +0.3 %
12 down arrow Matlab 1.74 % -0.1 %
13   Go 1.52 % +0.1 %
14 VBA 1.22 % -0.0 %
15 up arrow Rust 1.14 % +0.1 %
16 down arrow Ruby 1.13 % -0.0 %
17   Scala 0.84 % +0.0 %
18 up arrow Ada 0.78 % +0.2 %
19 down arrow Visual Basic 0.75 % -0.0 %
20 up arrow Dart 0.57 % +0.0 %
21 up arrow Abap 0.53 % +0.1 %
22 up arrow Julia 0.52 % +0.1 %
23 down arrow Lua 0.52 % -0.0 %
24 up arrow Groovy 0.43 % +0.0 %
25 Cobol 0.35 % -0.1 %
26 down arrow Perl 0.26 % -0.2 %
27 up arrow Delphi/Pascal 0.17 % -0.1 %
28 down arrow Haskell 0.0 % -0.3 %

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

TIOBE Index for September 2021

Sept 2021 Sept 2020 Change Programming Language Ratings Change
1 1 C 11.83% -4.12%
2 3 up arrow Python 11.67% +1.20%
3 2 down arrow Java 11.12% -2.37%
4 4 C++ 7.13% +0.01%
5 5 C# 5.78% +1.20%
6 6 Visual Basic 4.62% +0.50%
7 7 JavaScript 2.55% +0.01%
8 14 up arrow Assembly language 2.42% +1.12%
9 8 down arrow PHP 1.85% -0.64%
10 10 SQL 1.80% +0.04%
11 22 up arrow Classic Visual Basic 1.52% +0.77%
12 17 up arrow Groovy 1.46% +0.48%
13 15 up arrow Ruby 1.27% +0.03%
14 11 down arrow Go 1.13% -0.33%
15 12 down arrow Swift 1.07% -0.31%
16 16 MATLAB 1.02% -0.07%
17 37 up arrow Fortan 1.01% +0.65%
18 9 down arrow R 0.98% -1.40%
19 13 down arrow Perl 0.78% -0.53%
20 29 up arrow Delphi/Object Pascal 0.77% +0.24%

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 :



PHP: Tips of the Day

PHP: Pass a PHP string to a JavaScript variable (and escape newlines)

<script>
  var myvar = <?php echo json_encode($myVarValue); ?>;
</script>

Using json_encode() requires:

  • PHP 5.2.0 or greater
  • $myVarValue encoded as UTF-8 (or US-ASCII, of course)

Since UTF-8 supports full Unicode, it should be safe to convert on the fly.

Note that because json_encode escapes forward slashes, even a string that contains </script> will be escaped safely for printing with a script block.

Ref : https://bit.ly/34f0Dym