PHP

From Christoph's Personal Wiki
Jump to: navigation, search

PHP (PHP: Hypertext Preprocessor) is an open-source, reflective programming language. Originally designed as a high level scripting language for producing dynamic Web pages, PHP is used mainly in server-side application software.

Ternary Operator

Note: See Comparison Operators for details. Another conditional operator is the "?:" (or ternary) operator.

  • Assigning a default value:
<?php
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

// The above is identical to this if/else statement
if (empty($_POST['action'])) {
    $action = 'default';
} else {
    $action = $_POST['action'];
}
?>

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

Note: The ternary operator is a statement, and that it doesn't evaluate to a variable, but to the result of a statement. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions.

Sandbox

Inconsistent arguments and return values

To illustrate this problem, below is a table of the functions that match a user defined thing:

                         replaces case                 gives   s/m/x offset
                 matches with     insens number arrays matches flags (-1=end)
ereg             ereg             no     all    no     array   no    0
ereg_replace     ereg    str      no     all    no     no      no    0
eregi            ereg             yes    all    no     array   no    0
eregi_replace    ereg    str      yes    all    no     no      no    0
mb_ereg          ereg[1]          no     all    no     array   no    0
mb_ereg_replace  ereg[1] str/expr no     all    no     no      yes   0
mb_eregi         ereg[1]          yes    all    no     array   no    0
mb_eregi_replace ereg[1] str      yes    all    no     no      no    0
preg_match       preg[2]          yes/no one    no     array   yes   0
preg_match_all   preg             yes/no all    no     array   yes   0
preg_replace     preg    str/expr yes/no n/all  yes    no      yes   0
str_replace      str     str      no     all    yes    number  no    0
str_ireplace     str     str      yes    all    yes    number  no    0
strstr, strchr   str/char         no     one    no     substr  no    0
stristr          str/char         yes    one    no     substr  no    0
strrchr          char             no     one    no     substr  no    -1
strpos           str/char         no     one    no     index   no    n
stripos          str/char         yes    one    no     index   no    n
strrpos          char[3]          no     one    no     index   no    n
strripos         str              yes    one    no     index   no    -1
mb_strpos        str[1]           no     one    no     index   no    n
mb_strrpos       str[1]           yes    one    no     index   no    -1

[1] Handles multi-byte characters; [2] PCRE regex: so-called "Perl compatible" regular expressions; [3] Also does strings in PHP 5

Note: The same problem exists for other function groups, not just for matching. Also note: In Perl, all the functionality provided by the functions in this table is available through a simple set of 4 operators.

External links

Tips and tricks

PHP.ini