Going from Perl to php

I wrote this cross-reference a while back for people who know how to program using Perl and are looking for some of the familiar Perl functions in the php language. When I originally write it, we were stuck with php 3 so I just reworked the list to include some of the functions that were missing back then. There are obviously more differences between the two languages than the function-to-function cross-referencing that’s listed here but generally, it’s shouldn’t be a hard transition.

As a quick way to see more information on a function, you can use http://www.rahji.com/pod/functionname (where functionname is the perl function you want to look up). All it does is redirect your browser to the appropriate (maybe) page on perldoc.perl.org. For php functions, you can point your browser to http://www.php.net/functionname instead. Bookmark these.


lists and hashes
php doesn’t really discern between numerically indexed arrays (aka lists) and arrays indexed by key-strings (aka hashes aka associative arrays). Also remember that any kind of variable in php starts with a $, unlike perl which uses $ for scalars, @ for lists and % for hashes.
To check for the existence of a hash key in php, you should use array_key_exists().
In perl, you can have some chunk of code execute on each element of a list. In php, you’ll use the array_walk() function which requires that you create a function that will be applied to each item in the list.


In perl, it deletes a key and it’s associated value from a hash. In php, you probably want unset()
Woah there buddy, reset() does something different in php. In perl, it’s used to reset variables or ?PATTERN? searches – probably at the beginning of a loop. In php, it’s used to rewind an array back to the first element before you loop through it with a while loop or something.
In perl I’ve used it mainly for undefining a variable or a single element in a hash. In php, you’re probably looking for unset()

Pattern Matching

To perform perl-style regular expression substitutions in php, use preg_replace(). Look at its ability to accept lists as arguments. Pretty handy.
To do perl-style regular expression matches in php, use preg_match()
Use the strtr() function in php.


Use chop() or rtrim() (which are the same function) to remove whitespace and newlines at the end of a string. trim() and ltrim() come in handy too.
There isn’t a perl chop() in php. You’ll probably have to write your own function using substr().
lc, uc
These functions are called strtolower() and strtoupper() in php.
lcfirst, ucfirst
There’s no lcfirst() in php, but there is a ucfirst(). There’s also a ucwords().
php has quotemeta(), but it’s also got addslashes(), htmlentities(), htmlspecialchars(). Check out the mysql_real_escape_string()
q, qq, qw, qx
These quoting characters are really useful in perl, but don’t even think about it in php. This kind of loss of flexibility might seem frustrating at first, but in a weird way you get used to having less ways of doing the same thing. Less to remember? More uniform code? Don’t know.
ge, gt, le, lt, ne
To compare strings in php, use strcmp(). The function returns zero if the two arguments match, so you should use it like so: if (strcmp($this,$that)==0) print "match"; Don’t make this mistake: if (strcmp($this,$that)) print "duh";
repeating a string
In perl, you can print a bunch of dashes with something like print "-" x 20 but in php you have to use the str_repeat() function.

Control Structures

backwards if statements
You can’t do the cool perl backwards if statement (dosomething if condition) in php. You’ll have to be boring and do it with a normal if statement (if condition dosomething). There is, however, an alternate syntax for if statements that, like C, allows you to skip the curly braces.
Basically the same, except it’s do...while in php. While and foreach loops are pretty much the same in both languages too.
I get this wrong every time. It’s elseif (two e’s) in php. If you use an editor with syntax highlighting (like TextMate or vim) then it probably won’t be much of a problem for you.
Since php doesn’t use labels, it doesn’t have a goto. Probably not a bad thing.
This is called break in php. If you want to jump out more than one level, pass a numeric value to continue. That tells it how many levels to jump out, unlike perl where you’d use labels to do this. Also, note that even though the php documentation says that php considers an if statement a loop as far as this stuff goes, it’s a lie.
This is called continue in php. See the comments for last
Instead of defining subroutines, you define functions in php. The return() function is the same, though.
switch, case
perl doesn’t have a switch. php does.
I like this feature of perl. php doesn’t have an unless. You’ll have to use an ugly if statement.


2^3 (exponentiation)
In php, there’s no operator for this. Use the pow() function instead.
In php, it’s called hexdec(). Don’t forget printf() for formatting numbers. It’s identical in both languages.
rand, srand
php has these, but apparently mt_rand() and mt_srand() are better. Another thing is that rand/mt_rand take two arguments – a minimum and a maximum – which makes life easier.


To read an entire file into an array (each line in a separate array element) in php, use file()
Some of the file functions are the same as perl, except they have a letter ‘f’ in front of them. This is one of them: feof()
To create a link in php, use symlink()
open, close
Use fopen() and fclose() to open and close files in php. In php, you can open a url in the same way that you’d open a file. More function names that are the same as perl, but start with the letter ‘f’ — ftell(), fseek(), and fstat().
Use fputs() or fwrite() (which are the same function) to print to a filehandle in php.
Access the read system call with fread() in php.


php has exec() and system(), but you should also take a look at passthru().
I don’t think php has anything like the perl format stuff. Who cares?
php has a localtime(), but it doesn’t allow you to use it as a scalar like you can in perl. If you’re messing with dates and times, though, you should start by looking at the date() function. It allows you do display and manipulate dates and times in a way that’s only possible with modules in perl.

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 License.