Skip to main content
Share Your Experience: Take the 2024 Developer Survey
20 votes
Accepted

Display 0 - 1000 - 0 each on a separate line

PHP, 38 bytes for(;$i-=1|$x|=$i/1001;)echo~$i,"<p>"; Try it online! A one-byte improvement by JoKing. The idea is that by decrementing ...
Sisyphus's user avatar
  • 14.8k
9 votes

Tips for golfing in PHP

Use ${0}, ${1}, ${2}, ... instead of $a[0], $a[1], $a[2], ... Unless you're performing an array manipulation, most references to an array index $a[$i] can be ...
primo's user avatar
  • 33.3k
5 votes
Accepted

Need some feedback with PHP+C polyglot code and class/struct properties

You can add #define x $x with the other #defines at the top. Now $p->x should also work ...
user's user avatar
  • 327
5 votes

Tips for golfing in PHP

fun with typecasts !!$foo will turn any truthy value to true (or 1 in output), falsy values ...
Titus's user avatar
  • 14.7k
5 votes

Tips for golfing in PHP

looping through strings can be done with 26 bytes or with 24 down to 18: ...
Titus's user avatar
  • 14.7k
5 votes

Tips for golfing in PHP

by any other name ... function aliases use ... join instead of implode chop instead of <...
Titus's user avatar
  • 14.7k
4 votes

Tips for golfing in PHP

Combining for loops Suppose you have code of the following form: for($pre1; $cond1; $post1) for($pre2; $cond2; $post2) $code; ...
primo's user avatar
  • 33.3k
4 votes

Tips for golfing in PHP

array_flip vs array_search use array_flip($array)[$value] instead of array_search($value,$array) to save 1 Byte in ...
Jörg Hülsermann's user avatar
4 votes

Display 0 - 1000 - 0 each on a separate line

PHP,  56  49 bytes Saved 7 bytes thanks to @Sisyphus and @xnor My PHP is rusty, but here is a first attempt. ...
3 votes

Display 0 - 1000 - 0 each on a separate line

PHP, 51 bytes echo join("<p>",[...range(0,999),...range(1e3,0)]); Try it online! There's already shorter versions, but I just wanted to show the ...
Kaddath's user avatar
  • 2,907
3 votes

Tips for golfing in PHP

PCRE-Related (Regex) Functions If the pattern always matches the subject string (thanks to the comment by @Deadcode), instead of preg_replace, you can use ...
MAChitgarha's user avatar
3 votes

Tips for golfing in PHP

some interesting facts on variable variables I just had to share them (even before I verified that at least one of them helps golfing): Use letters: ...
Titus's user avatar
  • 14.7k
3 votes

Tips for golfing in PHP

Associative arrays can be merged with the + operator. Instead of: $merged = array_merge($a, $b); Use: ...
Alex Howansky's user avatar
3 votes

Tips for golfing in PHP

line breaks if the output requires line breaks, use a physical line break (1 byte) instead of "\n" This also gives you a possible benefit to chose between single ...
Titus's user avatar
  • 14.7k
2 votes

Tips for golfing in PHP

avoid quotes where possible PHP implicitly casts unknown words to literal strings. $foo=foo; is the same as $foo='foo'; (...
Titus's user avatar
  • 14.7k
2 votes

Tips for golfing in PHP

Arrow functions in PHP 7.4 PHP 7.4 is on RC2 version now and hopefully will be released in about 2 months. List of new features are here (this page can actually be updated when 7.4 is released). In 7....
Night2's user avatar
  • 5,957
2 votes

Tips for golfing in PHP

Use boolean operators instead of strtoupper() and strtolower() If you're working exclusively with strings consisting of alphabet ...
r3mainer's user avatar
  • 19.7k
2 votes

Tips for golfing in PHP

array_merge vs array_push array_push($a,...$b); is one byte shorter than $a=array_merge($a,$b); Does not work the same ...
Jörg Hülsermann's user avatar
1 vote

PHP+C polyglot solution to mixed types

This behaves the same in C and PHP (assuming there's a file named moo.txt with content "hej" in it). ...
Olle Härstedt's user avatar
1 vote

Shortest Code to Translate a PHP array

32 bytes foreach($a as$k=>&$v)$v="$k=$v"; Update by reference, shaves off 1 byte. Not sure how important removing the original key is, sort of a ...
ArtisticPhoenix's user avatar
1 vote

Tips for golfing in PHP

Little-known tricks with increment/decrement $x = 'a'; echo ++$x; // Outputs "b" $x = 'z'; echo ++$x; // Outputs "aa" And many more at the ...
mbomb007's user avatar
  • 23.4k
1 vote

Tips for golfing in PHP

unset() vs INF In a case search for a minimum in an array you can use instead of unset($var[$k]); $var[$k]=INF; to save 3 Bytes
Jörg Hülsermann's user avatar
1 vote

Tips for golfing in PHP

str_repeat In some cases you have a input of characters and you should output them repeated with an input greater zero for each characters. ...
Jörg Hülsermann's user avatar
1 vote

Tips for golfing in PHP

Removing characters in a string join(explode(" ",$string)); saves 1 character compared to ...
Jörg Hülsermann's user avatar
1 vote

Tips for golfing in PHP

intersecting strings Have you ever used join("DELIMITER",str_split($s)) (31 bytes) or even preg_replace(".","DELIMITER",$s) (32 ...
Titus's user avatar
  • 14.7k
1 vote

Tips for golfing in PHP

Return two copies of an expression If you want to get two copies of the result of an expression, you can use this: $v.$v=expr But it won't be shorter than the more ...
jimmy23013's user avatar
1 vote

Tips for golfing in PHP

double array_flip vs in_array vs array_unique in this special case a double array_flip saves 10 Bytes ($f=array_flip)($k=$f($c))) remove all double values in the ...
Jörg Hülsermann's user avatar
1 vote

Tips for golfing in PHP

Use end() instead of array_pop() The end() function doesn't just move the internal pointer ...
Alex Howansky's user avatar
1 vote

Tips for golfing in PHP

Use negative indexes to reference the end of a string If you need the last character in a string, you can use the array reference method, and provide a negative index: ...
Alex Howansky's user avatar
1 vote

Tips for golfing in PHP

Directly dereference arrays returned from functions. E.g., instead of this: $a = foo(); echo $a[$n]; You can do: ...
Alex Howansky's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible