PHP: General Concepts
Specification and History
- Not a W3C (World Wide Web Consortium) Specification - maintained through Open Source PHP Foundation and Zend.
- The former is responsible for driving development of the core Language/Runtime and the latter's responsible for commercializing PHP tooling, Certifications, etc.
- PHP was originally branding/stood for Personal Home Page. Now it stands for: PHP: Hypertext Preprocessor ('PHP' is recursively part of the official name of the Language).
- It's colloquially called 'PHP' in the same way that 'ECMAScript' and 'JavaScript' are used loosely and interchangeably.
Resources and Links
- https://www.php.net/manual/en/language.types.php
- https://thephp.foundation/
- https://www.zend.com/blog/zend-php-foundation
Code samples:
PHP: Gotcha's
Topics to review.
Types
Ten core Data Types spread across:
- Scalars
- Compound Types
- Special Types
- Pseudo Types
Scalars
Any of the Data Types:
boolintfloatstring
Compound Types
array- Associative (Key-Value), Indexed (standard), Multi-Dimenionsalobject- Class Instances (not Alias like in .NET forSystem.Object)
Special Types
null- Resource - Database
Pseudo Types
- Callable - invokable Functions (Anonymous or the String names of Functions)
- Iterable - Traversable Interface implementations (incl. Arrays) that are compatible with
foreach.
Arrays
$my_arr[] = 4;appends4to$my_arr.print_r($my_arr);to print contents of Array.
$my_arr = [1,2,3];
$my_arr[] = 4;
print_r($my_arr);
/*
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
*/
Constants
constant()allows one to get the Value of a Constant declared throughdefine()orconst.- Constants are Globally-scoped.
define()accepts any Expression and is evaluated at Runtime. Can be used anywhere.constaccepts only static Scalars, Arrays, and very basic Expressions. It must be used in specific places (Global Scope, Classes) and is checked at Compile Time.
SQL
- Both
MySQLiandPDOwill automatically close Connections at the end of the Script. mysql_fetch_array()still shows up on some Quizzes and Tests but it's long size been deprecated.
Magic Constants
$_REQUESThas all the contents of$_POST,$_GET, and$_COOKIE.- It's widely recommended by the PHP community to use
$_POST(Form Body) for large data inputs (rather than$_GET).
Operators
$x .= $yis the same as$x = $x . $y:$x = "a"; $y = "b"; $x .= $y; echo $x; //ab
Not Multiplication - String Concatenation.$x = "a"; $y = "b"; $x = $x . $y; echo $x; //ab
Uncaught Exceptions
- There's a question I encountered that's is a bit poorly worded. Like most languages PHP allows for optional Error Handling. If uncaught the Exception will bubble up to (optional) an Exception Handler (
set_exception_handler()).
Browser
get_browser()to describe the User's Browser capabilities.
Read and Write Permissions
For use with say fopen():
r+,w,w+,xall give Write Permission.x- Create and Open. Will throw if File already exists.w- Open for Writing only. Will create if File doesn't exist.
Cookies
setcookie()must be set before any HTML content (including<html></html>Tags) within the PHP Script.
Interfaces, Abstract Classes, Traits, Classes
traitsare akin to Mixins and represent functionalities that can be added to multiple Classes without them sharing Types.- Like Java in many respects:
extends,implementskeywords.- No multiple Inheritance.
- Overriding is done by exact Function Name and Signature match. (No Annotation/Decorator.)
- Interfaces can only have Constant Fields and Method Signatures.
self(specific Instance and its Class Definition Scope) vs$this(Class Definition).parent::my_method()- accesses parent Methods from Sub-Classes.static::my_method()- Overrides in the last Descendent Class at Runtime.
- Adapts
my_method()to Descendent Overrides at Runtime.
self::my_method()- Bound to the Instance's Class Definition Scope.
- Binds at Compile Time to the current Class where the keyword lexiographically resides.
- It completely ignores Descendent Class Overridding (can exhibit unexpected effects when Overridding and using
self).
::is the Scope Resolution Operator (akin to C++).
Resources and Links
- https://www.php.net/manual/en/language.types.array.php
- https://www.php.net/manual/en/mysqli.close.php
- https://www.php.net/manual/en/language.exceptions.php
- https://www.php.net/manual/en/function.get-browser.php
- https://www.php.net/manual/en/function.fopen.php
- https://www.php.net/manual/en/function.mysql-fetch-array.php
- https://www.php.net/manual/en/function.setcookie.php
- https://blog.masteringbackend.com/self-static-and-parent-in-php
- https://www.php.net/manual/en/language.oop5.static.php