-
Notifications
You must be signed in to change notification settings - Fork 0
php
Jorge Estanislao Barsoba edited this page Jul 17, 2024
·
9 revisions
- Created by Rasmus Lerdorf in 1994
- PHP is a recursive acronym that means "PHP Hypertext Preprocessor"
- Interpreted
- Dynamic (weak-typed), as type-checking is performed at runtime
- Nominal, as the name of the class determines the type of an object, like in C#; So, it's not Duck-typed
-
Type juggling: PHP does not support explicit type definition in variable declaration; i.e. If a string value is assigned to variable
$var,$varbecomes a string -
Type coercion: Automatic conversion of values from one data type to another, such as strings to numbers; e.g.
is_numeric("5.6")will return true (like JavaScript) - Type declarations: Can be added to function arguments, return values and, since v7.4, class properties
- String
- Integer
- Float
- Boolean
- Null
- Array
- Object
- Resource
$name = 'Jorge';
$age = 41;
$isMale = true;
$height = 1.81;
$salary = null;define('PI', 3.14);
echo PI;
var_dump(defined('PI')); // true
echo PHP_INT_MAX; // 9223372036854775807Predefined constants: https://www.php.net/manual/en/reserved.constants.php.
Constants that change their value depending on where they're called from, e.g. __DIR__, __FILE.
PHP-specific type of arrays that use named keys that you assign to them:
$age = array("Jorge" => 41, "Pau" => 42, "Male" => 4);JSON / Associative array utility functions:
// Convert JSON into Associative array
$json = file_get_contents('todo.json');
$jsonArray = json_decode($json, true);
$jsonArray[$todoName] = ['completed' => false];
// Convert Associative array back into JSON
file_put_contents('todo.json', json_encode($jsonArray, JSON_PRETTY_PRINT));Same as JavaScript:
-
==just checks the values, not the types -
===values and types
Predefined variables available in all scopes, such as $_SERVER.
-
/** ... */is a special syntax in PHP called a doc-block - Discoverable at runtime and used by different frameworks, e.g. Behat:
/** * @Given there is a(n) :arg1, which costs £:arg2 */ public function thereIsAWhichCostsPs($arg1, $arg2) { throw new PendingException(); }
When used on class declaration, it prevents that class from being extended. Use decorators instead of inheritance:
<?php
final class Foo
{
public method doFoo()
{
// do something useful and return a result
}
}
final class FooDecorator
{
private $foo;
public function __construct(Foo $foo)
{
$this->foo = $foo;
}
public function doFoo()
{
$result = $this->foo->doFoo();
// ... customize result ...
return $result;
}
}
?>In PHP, classes can implement the Countable interface and its count method:
final class Basket implements Countable {
private array $products;
...
public function count(): int {
return count($this->products);
}
}Then Basket will be countable:
PHPUnit_Framework_Assert::assertCount(intval($count), $this->basket);- Use VSCode keymap
- Override Ctrl+Shift+G (Git) to open Commit sidebar instead
- Disable conflicting shortcuts from Ubuntu
- Emoji shortcut (Ctrl+Shift+E) conflicts with "Open project": https://askubuntu.com/a/1159087
A \ before a class name or a function will ensure this is called from the global namespace, even if there's sth with the same name in the current namespace.