Difference between revisions of "PHP"

From Christoph's Personal Wiki
Jump to: navigation, search
(External links)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
'''PHP''' ('''P'''HP: '''H'''ypertext '''P'''reprocessor) 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.
 
'''PHP''' ('''P'''HP: '''H'''ypertext '''P'''reprocessor) 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.
  
== Sandbox ==
+
==Ternary Operator==
=== Inconsistent arguments and return values ===
+
''Note: See [http://us3.php.net/language.operators.comparison Comparison Operators] for details.''
 +
Another conditional operator is the "?:" (or ternary) operator.
 +
 
 +
*Assigning a default value:
 +
<pre>
 +
<?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'];
 +
}
 +
?>
 +
</pre>
 +
 
 +
The expression <code>(expr1) ? (expr2) : (expr3)</code> evaluates to <code>expr2</code> if <code>expr1</code> evaluates to <code>TRUE</code>, and <code>expr3</code> if <code>expr1</code> evaluates to <code>FALSE</code>.
 +
 
 +
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 <code>$var == 42 ? $a : $b</code>; 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:
 
To illustrate this problem, below is a table of the functions that match a user defined thing:
Line 40: Line 63:
 
*[http://particletree.com/features/database-simplicity-with-class/ Database Simplicity with Class]
 
*[http://particletree.com/features/database-simplicity-with-class/ Database Simplicity with Class]
 
*[http://phing.info/trac/ Phing]
 
*[http://phing.info/trac/ Phing]
 +
===Tips and tricks===
 +
*[http://blog.rightbrainnetworks.com/2006/09/18/10-things-you-probably-didnt-know-about-php/ 10 things you (probably) didn't know about PHP]
 +
===PHP.ini===
 +
*[http://www.askapache.com/php/custom-phpini-tips-and-tricks.html Custom PHP.ini tips and tricks] &mdash; from AskApache
  
 
[[Category:Scripting languages]]
 
[[Category:Scripting languages]]
 
[[Category:World Wide Web]]
 
[[Category:World Wide Web]]

Latest revision as of 23:07, 30 May 2012

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