This is a preview

This playground version isn't public and is work in progress.

PHP

PHP

Checking the sample code

Our sample puzzle solution looks like this in PHP:

Looking at the syntax

Let's check this code to see the main characteristics of PHP:

  • <?php PHP was originally designed as a server side script language for web pages. HTML markup and PHP code can me mixed in a file, so <?php denotes the start of a php section.
  • declare(strict_types=1); PHP is infamous for its type-jugling and all the weird bugs it can bring to your code. Luckily, in the past years PHP moved heavily to a stronger type system, including type hints for function arguments, return types and class properties, etc. I recommend starting all your code with this declare line to enforce the stronger type checking.
  • After C#'s using and Java's import you might miss some library references here. PHP does have an extensive library, but exactly what is available for the coder is installation-specific. In the sample code strlen, str_pad, decbin, etc. are all library functions.
  • $m = "CG";
    • The $ here does not denote how mush a professional PHP coder earns, but all variable names start with it. It needs some getting used to (unless you came from Perl...).
    • PHP has a dynamic type system. Here, $m is assigned a string literal, so it becomes a string, but later we could simple reassign it to an integer. (It is not the best practice to do so.)
  • const c = ['0' => '00', '1' => '0'];
    • PHP is also infamous for its inconsistency. Here c is declared to be immutable by adding const, but that results, that you must not use the $ prefix anymore.
    • The assigned literal data structure is PHP's very flexible array. It is so flexible, that this is almost all that PHP has to offer. Internally, it is implemented as an ordered hashmap, and you can use it as a vector, stack, queue, dictionary, map, etc. Having to use a complex data type, even if you need just a fixed-size vector of integers, is many times an overkill, and it largely adds to the slowness of PHP.
  • Why are there no parentheses around the parameters passed to echo? Well, it could have been written as echo($ans), but echo is not a normal library function, it is a language construct.

Other characteristics

  • While not visible from the sample code above, PHP supports multiple programming paradigms. Besides procedural style, it can be used in an object-oriented way. Although not really a functional language, some aspects of functional programming are also well supported.
  • PHP is interpreted. You can run your script without a separate compilation phase. This also means, that PHP is quite SLOW. Although all solo puzzles can be solved on CodinGame by choosing the right algorithm, but it does not stand a chance in a CG contests against compiled codes (e.g. C++, Rust, etc.). PHP v8.0 provides Just-In-Time compilation, resulting 3-5x speedup for a typical CG puzzle code (but not for a typical web application!), unfortunately CG does not support this version yet.

Resources to check

Meme

Coming next...

Okay, I have to admit I was a bit biased towards PHP, because that is the language I know the most. That is why we started the exploration of interpreted languages with it, despite of the undeniable fact, that it is perceived as less fancy than it was 10+ years ago.

No let's check another weakly typed, interpreted language, which became the de-facto standard language of web front-ends: JavaScript!

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content