Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
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;
Enter to Rename, Shift+Enter to Preview
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 (and?>
would mark the end of it, but we can omit it as our full script is in PHP).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 towards a stronger type system, including type hints for function arguments, return types and class properties, etc. Not to break backward compatibility, this is fully opt-in. I recommend starting all your code with thisdeclare
line to enforce the stronger type checking, and to use all the type declaration facilities.- 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 code,strlen
,str_pad
,decbin
, etc. are all library functions. $m = "CG";
- The
$
here does not denote how much a professional PHP coder earns, but all variable names start with it. Yes, 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. (However, to do so is not the best practice.)
- 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 a hashmap with the key order preserved. You can use it as avector
,stack
,queue
,dictionary
,map
, etc. Having to use such a complex data type, even if you would need just a fixed-size array of integers, is in many cases an overkill. This design decision 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.PHP_EOL
is just an OS-dependent constant for the endline, so CR + LF in Windows and LF in Linux.
Other characteristics
- While not visible from the sample code above, PHP supports multiple programming paradigms.
- Besides the procedural style, it can be used in an object-oriented way. Some standard libraries (such as database access) have even dual interfaces: you can use them either by calling simply functions or by instantiating classes and calling methods.
- The most popular PHP web frameworks such as Laravel or Symfony are all written in OOP.
- Although not really a functional language, some aspects of functional programming are also well supported. (Higher order functions, lambda functions, etc.).
- PHP is interpreted. You can run your script without a separate compilation phase. This also results that PHP is quite slow. Although all solo puzzles can be solved on
CodinGame
by choosing the right algorithm, but PHP does not stand much chance in a CG contest 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, at the time of writing this, 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. My personal opinion is still that it is much better than its fame.
Now let's check another weakly typed, interpreted language, which became the de-facto ruler of web front-ends: JavaScript!
Suggested playgrounds
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content