Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Perl and PHP are more different than alike. Let's consider Perl 5, since Perl 6 is still under development. Some differences, grouped roughly by subject:</p> <ul> <li>Perl has native regular expression support, including regexp literals. PHP uses Perl's regexp functions as an extension.</li> <li>Perl has quite a few more <a href="http://perldoc.perl.org/perlop.html" rel="noreferrer">operators</a>, including <a href="http://perldoc.perl.org/perlop.html#Binding-Operators" rel="noreferrer">matching</a> (<code>=~</code>, <code>!~</code>), <a href="http://perldoc.perl.org/perlop.html#Quote-Like-Operators" rel="noreferrer">quote-like</a> (<code>qw</code>, <code>qx</code> &amp;c.), <a href="http://perldoc.perl.org/perlop.html#Exponentiation" rel="noreferrer">exponentiation</a> (<code>**</code>), <a href="http://perldoc.perl.org/perlop.html#Multiplicative-Operators" rel="noreferrer">string repetition</a> (<code>x</code>) and <a href="http://perldoc.perl.org/perlop.html#Range-Operators" rel="noreferrer">range</a> (<code>..</code> and <code>...</code>). PHP has a few operators Perl doesn't, such as the <a href="http://us.php.net/manual/en/language.operators.errorcontrol.php" rel="noreferrer">error suppression operator</a> (<code>@</code>), <a href="http://php.net/instanceof" rel="noreferrer"><code>instanceof</code></a> (though Perl does have the <code><a href="http://search.cpan.org/~dapm/perl-5.10.1/lib/UNIVERSAL.pm" rel="noreferrer">Universal</a>::isa</code> method) and <a href="http://en.wikipedia.org/wiki/Sigil_(computer_programming)" rel="noreferrer"><code>clone</code></a>. </li> <li>In PHP, <a href="http://php.net/new" rel="noreferrer"><code>new</code></a> is an operator. In Perl, it's the conventional name of an <a href="http://perldoc.perl.org/perltoot.html#Constructors-and-Instance-Methods" rel="noreferrer">object creation subroutine</a> defined in packages, nothing special as far as the language is concerned.</li> <li><p>Perl logical operators return their arguments, while they <a href="http://www.php.net/manual/en/language.operators.logical.php" rel="noreferrer">return booleans</a> in PHP. Try:</p> <pre><code>$foo = '' || 'bar'; </code></pre> <p>in each language. In Perl, you can even do <code>$foo ||= 'default'</code> to set $foo to a value if it's not already set. The shortest way of doing this in PHP is <code>$foo = isset($foo) ? $foo : 'default';</code> (Update, in PHP 7.0+ you can do <code>$foo = $foo ?? 'default'</code>)</p></li> <li>Perl <a href="http://perldoc.perl.org/perldata.html#Variable-names" rel="noreferrer">variable names</a> indicate built-in type, of which Perl has three, and the type specifier is part of the name (called a "<a href="http://en.wikipedia.org/wiki/Sigil_(computer_programming)" rel="noreferrer">sigil</a>"), so <code>$foo</code> is a different variable than <code>@foo</code> or <code>%foo</code>.</li> <li>(related to the previous point) Perl has separate <a href="http://www252.pair.com/comdog/mastering_perl/Chapters/08.symbol_tables.html" rel="noreferrer">symbol table</a> entries for scalars, arrays, hashes, code, file/directory handles and formats. Each has its own namespace.</li> <li>Perl gives access to the <a href="http://perldoc.perl.org/perlmod.html#Symbol-Tables" rel="noreferrer">symbol table</a>, though manipulating it isn't for the faint of heart. In PHP, symbol table manipulation is limited to creating <a href="http://php.net/references" rel="noreferrer">references</a> and the <a href="http://php.net/extract" rel="noreferrer"><code>extract</code></a> function.</li> <li>Note that "references" has a different meaning in PHP and Perl. In PHP, <a href="http://php.net/references" rel="noreferrer">references</a> are symbol table aliases. In Perl, <a href="http://perldoc.perl.org/perlref.html" rel="noreferrer">references</a> are smart pointers.</li> <li>Perl has different types for integer-indexed collections (arrays) and string indexed collections (hashes). In PHP, they're the same type: an <a href="http://php.net/array" rel="noreferrer">associative array/ordered map</a>.</li> <li>Perl arrays aren't sparse: setting an element with index larger than the current size of the array will set all intervening elements to <code>undefined</code> (see <a href="http://perldoc.perl.org/perldata.html" rel="noreferrer">perldata</a>). PHP arrays are sparse; setting an element won't set intervening elements.</li> <li>Perl supports hash and array <a href="http://perldoc.perl.org/perldata.html#Slices" rel="noreferrer">slices</a> natively, and slices are assignable, which has all sorts of <a href="https://stackoverflow.com/questions/1242040/how-can-i-join-two-hashes-in-perl-without-using-a-loop/1242125#1242125">uses</a>. In PHP, you use <a href="http://php.net/array_slice" rel="noreferrer"><code>array_slice</code></a> to extract a slice and <a href="http://php.net/array_splice" rel="noreferrer"><code>array_splice</code></a> to assign to a slice.</li> <li>You can leave out the <a href="http://php.net/manual/en/language.types.array.php#language.types.array.syntax.modifying" rel="noreferrer">argument to the subscript operator</a> in PHP for a bit of magic. In Perl, you can't leave out the subscript.</li> <li>Perl hashes are <a href="http://perldoc.perl.org/perlglossary.html#hash" rel="noreferrer">unordered</a>.</li> <li>Perl has a large number of <a href="http://perldoc.perl.org/perlvar.html" rel="noreferrer">predefined and magic variables</a>. PHP's <a href="http://www.php.net/manual/en/language.variables.predefined.php" rel="noreferrer">predefined variables</a> have quite a different purpose.</li> <li>Perl has <a href="http://perldoc.perl.org/perlsyn.html#Statement-Modifiers" rel="noreferrer">statement modifiers</a>: some control statements can be placed at the end of a statement.</li> <li>Perl supports <a href="http://en.wikipedia.org/wiki/Scope_(programming)#Dynamic_scoping" rel="noreferrer">dynamic scoping</a> via the <code>local</code> keyword.</li> <li>In addition, Perl has global, lexical (block), and package <a href="http://docstore.mik.ua/orelly/perl/prog3/ch04_08.htm" rel="noreferrer">scope</a>. PHP has global, function, object, class and namespace <a href="http://php.net/manual/en/language.variables.scope.php" rel="noreferrer">scope</a>. </li> <li>In Perl, variables are global by default. In PHP, variables in functions are local by default.</li> <li>Perl supports explicit <a href="http://en.wikipedia.org/wiki/Tail_call" rel="noreferrer">tail calls</a> via the <a href="http://perldoc.perl.org/functions/goto.html" rel="noreferrer"><code>goto</code></a> function.</li> <li>Perl's <a href="http://perldoc.perl.org/perlsub.html#Prototypes" rel="noreferrer">prototypes</a> provide more limited type checking for function arguments than PHP's <a href="http://php.net/manual/en/language.oop5.typehinting.php" rel="noreferrer">type hinting</a>. As a result, prototypes are of more limited utility than type hinting.</li> <li>In Perl, the last evaluated statement is returned as the value of a subroutine if the statement is an expression (i.e. it has a value), even if a return statement isn't used. If the last statement isn't an expression (i.e. doesn't have a value), such as a loop, the return value is unspecified (see <a href="http://perldoc.perl.org/perlsub.html" rel="noreferrer">perlsub</a>). In PHP, if there's no explicit return, the <a href="http://www.php.net/manual/en/functions.returning-values.php" rel="noreferrer">return value is NULL</a>.</li> <li><p>Perl flattens lists (see <a href="http://perldoc.perl.org/perlsub.html" rel="noreferrer">perlsub</a>); for un-flattened data structures, use references. </p> <pre><code>@foo = qw(bar baz); @qux = ('qux', @foo, 'quux'); # @qux is an array containing 4 strings @bam = ('bug-AWWK!', \@foo, 'fum'); # @bam contains 3 elements: two strings and a array ref </code></pre> <p>PHP doesn't flatten arrays.</p></li> <li>Perl has <a href="http://perldoc.perl.org/perlmod.html#BEGIN%2c-UNITCHECK%2c-CHECK%2c-INIT-and-END" rel="noreferrer">special code blocks</a> (<code>BEGIN</code>, <code>UNITCHECK</code>, <code>CHECK</code>, <code>INIT</code> and <code>END</code>) that are executed. Unlike PHP's <a href="http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file" rel="noreferrer"><code>auto_prepend_file</code></a> and <a href="http://www.php.net/manual/en/ini.core.php#ini.auto-append-file" rel="noreferrer"><code>auto_append_file</code></a>, there is no limit to the number of each type of code block. Also, the code blocks are defined within the scripts, whereas the PHP options are set in the server and per-directory config files.</li> <li>In Perl, the semicolon <a href="http://perldoc.perl.org/perlsyn.html#Simple-Statements" rel="noreferrer">separates statements</a>. In PHP, it <a href="http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php" rel="noreferrer">terminates</a> them, excepting that a PHP close tag ("?>") can also terminate a statement.</li> <li>The value of expressions in Perl is <a href="http://perldoc.perl.org/perldata.html#Context" rel="noreferrer">context sensitive</a>.</li> <li>Negative subscripts in Perl are relative to the end of the array. <code>$bam[-1]</code> is the final element of the array. Negative subscripts in PHP are subscripts like any other.</li> <li>In Perl 5, classes are based on packages and look nothing like classes in PHP (or most other languages). Perl 6 classes are closer to PHP classes, but still quite different. (Perl 6 is <a href="http://perlcabal.org/syn/Differences.html" rel="noreferrer">different</a> from Perl 5 in many other ways, but that's off topic.) Many of the differences between Perl 5 and PHP arise from the fact that most of the OO features are not built-in to Perl but based on hacks. For example, <code>$obj-&gt;method(@args)</code> gets translated to something like <code>(ref $obj)::method($obj, @args)</code>. Non-exhaustive list: <ul> <li>PHP automatically provides the special variable <code>$this</code> in methods. Perl passes a reference to the object as the first argument to methods.</li> <li>Perl requires references to be <a href="http://perldoc.perl.org/functions/bless.html" rel="noreferrer">blessed</a> to create an object. Any reference can be blessed as an instance of a given class.</li> <li>In Perl, you can dynamically change inheritance via the packages <code>@ISA</code> variable.</li> </ul></li> <li>Perl supports <a href="http://perldoc.perl.org/overload.html" rel="noreferrer">operator overloading</a>.</li> <li>Strictly speaking, Perl doesn't have multiline comments, but the <a href="http://perldoc.perl.org/perlpod.html" rel="noreferrer">POD</a> system can be used for the same affect.</li> <li>In Perl, <code>//</code> is an operator. In PHP, it's the start of a one-line comment.</li> <li>Until PHP 5.3, PHP had terrible support for anonymous functions (the <code>create_function</code> function) and no support for closures.</li> <li>PHP had nothing like Perl's packages until version 5.3, which introduced <a href="http://www.php.net/manual/en/language.namespaces.php" rel="noreferrer">namespaces</a>.</li> <li>Arguably, Perl's built-in support for exceptions looks almost nothing like exceptions in other languages, so much so that they scarcely seem like exceptions. You evaluate a block and check the value of <code>$@</code> (<code>eval</code> instead of <code>try</code>, <a href="http://perldoc.perl.org/functions/die.html" rel="noreferrer"><code>die</code></a> instead of <code>throw</code>). The <a href="http://search.cpan.org/~shlomif/Error-0.17016/lib/Error.pm" rel="noreferrer"><s>Error</s></a> <a href="http://p3rl.org/Try::Tiny" rel="noreferrer">Try::Tiny</a> module supports exceptions as you find them in other languages (as well as some other modules listed in <a href="https://metacpan.org/module/Error#SEE-ALSO" rel="noreferrer">Error's See Also</a> section).</li> </ul> <p>PHP was inspired by Perl the same way <em>Phantom of the Paradise</em> was inspired by <em>Phantom of the Opera</em>, or <em>Strange Brew</em> was inspired by <em>Hamlet</em>. It's best to put the behavior specifics of PHP out of your mind when learning Perl, else you'll get tripped up.</p> <p>My brain hurts now, so I'm going to stop.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload