Brennen Bearnes 14082e4737 | 4 years ago | |
---|---|---|
.. | ||
Makefile | 4 years ago | |
README.md | 4 years ago | |
render.pl | 4 years ago |
% PHP for the Reluctant % Brennen Bearnes % 2020-08-28
::: incremental
:::
::: incremental
:::
::: incremental
:::
The simplest example in PHP is just a file containing a string literal:
Hello world.
<?php
tag, the interpreter will just print everything it encounters.For a more traditional hello world, you'd write:
<?php
print "Hello world.\n";
Or maybe:
<?php
$name = 'world';
?>
Hello, <?= $name ?>.
$
: $foo
, $bar
, $baz
$
, and conveys no type information (unlike Perl, etc.)[A-Za-z0-9_]
These are your options:
<?php
// Global scope:
$foo = 1;
function bar () {
// Local to function or method:
$foo = 2;
return $foo;
}
function baz () {
// Access the $foo in global scope:
global $foo;
$foo = 3;
}
print "$foo\n";
print bar() . "\n";
baz();
print "$foo\n";
<?php
// Define a constant - all-caps by convention:
define('NAME', 'VALUE');
print NAME;
// Barewords can be risky since they're treated as string literals
// for undefined constants. You can use constant() instead:
print constant('NAME');
<?php
// Literals, case-insensitive:
$are_cats_mammals = true;
$are_elephants_reptiles = false;
<?php
// Floating point:
$foo = 1.234;
// Integer:
$foo = 1;
You'll rarely encounter any but the first syntax for these, but they do work:
<?php
// Floating point:
$foo = 1.234;
$foo = 1.2e3;
$foo = 7E-10;
# $foo = 1_234.567;
// Integer:
$foo = 1;
$foo = 0123; // octal number (equivalent to 83 decimal)
$foo = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$foo = 0b11111111; // binary number (equivalent to 255 decimal)
# $foo = 1_234_567; // decimal number (as of PHP 7.4.0)
<?php
$var = 'variable';
$double = "double-quoted with $var interpolation and escapes.\n";
$single = 'single-quoted';
// String concatenation:
print $double . "\n" . $single;
An alternative string quoting mechanism, a la shell or Perl:
<?php
$foo = <<<EOT
bar
EOT;
To avoid variable interpolation and escapes, enclose the end marker in single quotes:
<?php
echo <<<'EOD'
Example of string spanning multiple lines
using nowdoc syntax. Backslashes are always treated literally,
e.g. \\ and \'.
EOD;
<?php
// Old syntax:
$arr = array(1, 2, 3);
// Short syntax:
$arr = [1, 2, 3];
print_r($arr);
<?php
$foo = ['one', 'two', 'three'];
print_r($foo);
// Also perfectly legal - mixing types:
$foo = ['one', 2, 3.0];
print_r($foo);
<?php
$relenger_nicks = [
'dancy' => 'Ahmon Dancy',
'brennen' => 'Brennen Bearnes',
'liw' => 'Lars Wirzenius',
'longma' => 'Jeena Huneidi',
];
<?php
$relengers = [
[
'nick' => 'dancy',
'name' => 'Ahmon Dancy',
'editors' => ['emacs', 'vim']
],
[
'nick' => 'brennen',
'name' => 'Brennen Bearnes',
'editors' => ['vim', 'nano', 'edit.com']
],
];
print $relengers[1]['name'] . ' uses ' . $relengers[1]['editors'][0];
Type coercion is common, and (unsurprisingly) a frequent cause of unexpected behavior. (More about that in a minute.)
Types can be explicitly cast like so:
<?php
$foo = true;
$foo_float = (float)$foo;
$foo_int = (int)$foo;
$foo_str = (string)$foo;
$foo_bool = (bool)$foo;
<?php
// Things that evaluate to false:
false, 0, -0, 0.0, -0.0, '0', '', null, []
// ...plus undefined variables.
<?php
abstract class Encabulator {
abstract public function reticulate ();
}
class TurboEncabulator extends Encabulator {
protected $_splinesReticulated = false;
public function reticulate () {
$this->_splinesReticulated = true;
}
}
$te = new TurboEncabulator();
$te->reticulate();
With a basic overview of the language out of the way, let's go over some common pitfalls and sources of recurring bugs.
<?php
$arr = [
'foo' => 'bar',
];
$baz = $arr['fo'];
<?php
$foo->method();