PHP: Difference between revisions

From miki
Jump to navigation Jump to search
Line 49: Line 49:


== Syntax ==
== Syntax ==
Some data types:
<source lang=php>
<source lang=php>
/* boolean */
$x = true;
$y = false;

/* integers */
$i = 42;

/* arrays */
/* arrays */
$attr = array(); // an empty array
$attr = array(); // an empty array
$cars = array("Volvo","BMW","Toyota");
$attr = array("border"=>"myborder", "mode"=>"mymode"); // associative array
$attr = array("border"=>"myborder", "mode"=>"mymode"); // associative array


Line 59: Line 68:
$s=str_replace("\n","",$s); // Replace a substring in a string
$s=str_replace("\n","",$s); // Replace a substring in a string
$s=str_replace(array("\r","\n","\t"," "),"",$s; // ... idem, with several substrings (also support several "replace" strings)
$s=str_replace(array("\r","\n","\t"," "),"",$s; // ... idem, with several substrings (also support several "replace" strings)
</source>


Some operators:
<source lang=php>
/* arithmetic */
$x + $y;
$x - $y;
$x * $y;
$x / $y;
$x % $y;
$x ** $y; // exponentiation

/* assigment */
$x = $y;
$x += $y;A // ... and all others: -= *= /= %=

/* comparison */
$x == $y; // ... and all others: != > < >= <=
$x <> $y; // same as !=
$x === $y; // identical operator (are equal and of the same type)
$x !== $y; // not identical operator (are not equal or are not of the same type)

/* increment / decrement */
$x++; // and $x--
++$x; // and --$x

/* logical */
$x and $y; // or '&&'
$x or $y; // or '||'
$x xor $y;
!$x;

/* string */
$x . $y; // concatenation
$x .= $y; // concatenation assignment

/* array */
$x + $y; // union
$x == $y; // equality
$x === $y; // identity
$x != $y; // inequality, or '<>'
$x !== $y; // non-identity
</source>

<source lang=php>
/* OO */
/* OO */



Revision as of 17:20, 22 December 2017

References

Cheat sheets:

Install

On Windows

Download the zip archive on http://windows.php.net/download/:

To install
  • Create a directory C:\php and unzip archive inside.
  • Add C:\php to system environment path PATH.
  • Copy C:\php\php.ini-production to C:\php.ini.
Alternatively use the development version for more debug. But production ini is handy to mute deprecated API warnings
Enable a few extensions
  • To use HTTPS or SSL, uncomment the line:
extension=php_openssl.dll
  • To use SOAP, uncomment the line:
extension=php_soap.dll
  • To use Curl, uncomment the line:
extension=php_curl.dll
  • To use XML RPC, uncomment the line:
extension=php_xmlrpc.dll
Add/change certificate CA
  • Edit php.ini:
openssl.cafile=C:/path/to/sslCertificates/cacert.pem

Invocation

php -a                  # Start PHP in interactive mode

Syntax

Some data types:

/* boolean */
$x = true;
$y = false;

/* integers */
$i = 42;

/* arrays */
$attr = array();          // an empty array
$cars = array("Volvo","BMW","Toyota");
$attr = array("border"=>"myborder", "mode"=>"mymode");   // associative array

/* strings */
'abc' . 'def'                                     // concatenation
"abc $var def"                                    // variable reference in strings
$s=str_replace("\n","",$s);                       // Replace a substring in a string
$s=str_replace(array("\r","\n","\t"," "),"",$s;   // ... idem, with several substrings (also support several "replace" strings)

Some operators:

/* arithmetic */
$x + $y;
$x - $y;
$x * $y;
$x / $y;
$x % $y;
$x ** $y;    // exponentiation

/* assigment */
$x = $y;
$x += $y;A   // ... and all others: -= *= /= %=

/* comparison */
$x == $y;    // ... and all others: != > < >= <=
$x <> $y;    // same as !=
$x === $y;   // identical operator (are equal and of the same type)
$x !== $y;   // not identical operator (are not equal or are not of the same type)

/* increment / decrement */
$x++;        // and $x--
++$x;        // and --$x

/* logical */
$x and $y;   // or '&&'
$x or $y;    // or '||'
$x xor $y;
!$x;

/* string */
$x . $y;     // concatenation
$x .= $y;    // concatenation assignment

/* array */
$x + $y;     // union
$x == $y;    // equality
$x === $y;   // identity
$x != $y;    // inequality, or '<>'
$x !== $y;   // non-identity
/* OO */

self::$static_property;
$this->property;
self::CONSTANT;

/* special keywords */
self;              // The current class
$this;             // The current object
NULL;              // The null reference

/* magic constants -- http://php.net/manual/en/language.constants.predefined.php */
__FILE__	   // The full path and filename of the file with symlinks resolved
__DIR__            // Equivalent to dirname(__FILE__). Since PHP 5.3.0

/* Scope */
global $wgScriptPath; // Declare $wgScriptPath as global when in a local scope

Unicode support

  • First, if PHP file contains special character, make sure the PHP file is encoded in UTF-8:
file -bi FenTT.php           # To get current encoding
iconv -f $encoding -t UTF-8 -o FenTT.out.php FenTT.php        # Specify correct source $encoding as given above (note that converting us-ascii is useless)
  • Specify the default multi-byte encoding:
mb_internal_encoding("UTF-8");
  • Next store unicode char as follows [1]:
$S = '\u2654\u2655\u2656\u2657';
$S = json_decode('"'.$S.'"');
  • Then access the string using the multi-byte API [2], like [3]:
$D = mb_substr($S,2,1);    // instead of $D=$S[2]

See php reference - string details for more information on how strings are handled internally in php.