PHP is a web development language (Think CSS and HTML) that can also be used as a regular programming language. As such, it fits rather strangely right in the middle of both. Coding in PHP can be done in many different ways, and one of the reasons it's fun to golf in, is that PHP is very lax about how it lets you do things.
PHP has weak typing, so a variable when created can be assigned any value you please. As well, type-casting is automatic in most situations, so throwing a string where a number is expected often works. PHP hunts for the first number in the string to use (
"123Hello" = 123), or treats it as a 0 if there's no number. And since PHP has a different operator for concatenation of strings, we don't have to worry about accidental typecasting when we don't want it.
"1" + "2" = 3
"1" . "2" = "12"
Numbers can also be used as strings in the same way!
1 . 2 = "12"
Wanna declare a variable? Do it whenever, wherever.
$string = 0;
$number = "2";
Wanna use a variable? Do it whenever, wherever.
// No variables initialized
$a = $n + $m; // Where the hell did these come from, then?
You see, PHP doesn't break if you try to use a variable without a value. It puts out a little warning, but otherwise runs perfectly fine! This allows for a lot of saved bytes thanks to not needing to declare variables before using them
$string = ""; // This first part is completely unnecessary!
$string .= "Hello " // PHP just kinda assumes you're working with a blank string anyways if there's nothing there.
$string .= "world!"
$number++; // No number, no problem! We'll just make that a 0 and then increment it...
All arrays in PHP are associative arrays, similar to a Dictionary in C# or a Map in Java. Keys and values for arrays can be almost anything, and there's no set size for an array.
$array =  // A blank array
$array = [1, "2", [3, 4]] // Some values to start
$array = false // Numeric keys are the default...
$array['fruit'] = "banana"// But string keys are fine too!
To add a value to an array, you simply set the position to whatever you want. No fuss about pre-initializing anything, the same as variables.
$array =  // We start with a blank array...
$array = 2 // And can start wherever we want.
$array['red'] = 'blue' // Positions are created as needed.
$array = 'last' // Not supplying a key automatically adds the given value to the end of the array
Oh, did I mention strings can be referenced like arrays?
$string = "Hello world!" // String would give us "l"
$string = "," // Our string is now "Hello,world!"
PHP also comes with a whole bevy of built-in array functions, which can all be viewed here
PHP can compare any two values to evaluate a logical condition. You can see here what happens if you compare various types of values against each other.
Conveniently, PHP doesn't require you to explicitly do the evaluation! It's quite happy with truthy/falsy values, doing away with pesky things like
$string != "" in favor of just
Conditionals and Looping
PHP supports all the traditional constructs (
switch) as well as a very convenient
foreach syntax which allows you to keep track of keys and values as you go.
foreach ($array as $value) // The simple syntax when you just want the values
foreach ($array as $key => $value) // But if you want the key, it's easy!
PHP also supports ternary operator (
?:) as a shorthand for if/else syntax.
$a = $boolean ? $b : $c; // If our boolean is true, $b is assigned to $a, otherwise $c is.
$boolean ? something() : somethingElse(); // Doesn't have to be used solely in assignment, either.
$string = $me_if_not_false ?: $me_if_true // The shorthand version 'returns' the value used for logical evaluation if it's truthy.
PHP, being a web-based language, makes it really easy to output to the screen.
echo $variable; // Nice and simple.
echo $variable . "\n"; // With a newline.
echo $variable . "<br/>";// For you HTML nuts.
It's just that easy. (Warning, doesn't work with arrays, but check out
From one of the challenges I posted in, Count without 3
for($i=$argv // We initialize $i because it's shorter ($argv is the first argument passed from the command line)
; // Our condition for ending the for loop starts here.
!(++$i%3) // Counting up from $i, we end the loop if it's not a multiple of 3 (if $i mod 3 equals 0).
|strpos(_.$i,'3');) // Treating $i like a string, we also end the loop if we find '3' anywhere in it.
; // This closes the for loop, as we don't need anything done during the loop that we're not already doing.
echo$i;// After our loop ends, we output $i to the screen.
PHP is, in my opinion, a very fun language to golf in. Although it has some shortcomings compared to other languages (each variable is 2 bytes instead of 1,
verbose built-in functions), it can be quite the brain twister to try and
abuse all the cuttable corners it offers, and in the end can be quite competitive!
I hope you have fun golfing in PHP!