A Babel of Languages on CodinGame
TBali
66.8K views
PHP
Checking the sample code
Our sample puzzle solution looks like this in PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
declare(strict_types=1);
// $m = stream_get_line(STDIN, 100, "\n");
$m = "CG";
const c = ['0' => '00', '1' => '0'];
$b = '';
for ($i = 0; $i < strlen($m); $i++) {
$b .= str_pad(decbin(ord($m[$i])), 7, '0', STR_PAD_LEFT);
}
$ans = c[$b[0]] . ' 0';
for ($i = 1; $i < strlen($b); $i++) {
if ($b[$i] == $b[$i - 1])
$ans .= '0';
else
$ans .= ' ' . c[$b[$i]] . ' 0';
}
// error_log(var_export($var, true));
echo $ans, PHP_EOL;
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 thisdeclare
line to enforce the stronger type checking.- After C#'s
using
and Java'simport
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 codestrlen
,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.)
- The
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 avector
,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.
- PHP is also infamous for its inconsistency. Here c is declared to be immutable by adding
- Why are there no parentheses around the parameters passed to
echo
? Well, it could have been written asecho($ans)
, butecho
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
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!
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.