Study Guide 2023+

php

Warning: These notes are partial, ongoing, incomplete, and may contain typos/inaccuracies. (They are kept factually accurate, time permitting.)

They are being united from many disparate notes created in the past and the layout/organization will gradually improve with time!

Please view them on a computer as they are not optimized for mobile (although you can still view them on Mobile along with the Flashcards at your own risk)!

Topics and code examples are lazy-loaded and may require two-clicks from the TOC to correctly calculate the updated x,y coordinates (after rendering). Thanks!

PHP: General Concepts

Specification and History

  1. Not a W3C (World Wide Web Consortium) Specification - maintained through Open Source PHP Foundation and Zend.
  2. The former is responsible for driving development of the core Language/Runtime and the latter's responsible for commercializing PHP tooling, Certifications, etc.
  3. 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).
  4. It's colloquially called 'PHP' in the same way that 'ECMAScript' and 'JavaScript' are used loosely and interchangeably.

https://www.zend.com/blog/zend-php-foundation

  1. https://www.php.net/manual/en/language.types.php
  2. https://thephp.foundation/
  3. https://www.zend.com/blog/zend-php-foundation

Code samples:

  1. https://github.com/Thoughtscript/php_2024
  2. https://github.com/Thoughtscript/x_team_wp_react
  3. https://web.archive.org/web/20240628022553/https://x-team.com/blog/cms-to-waf-wordpress-and-react
  4. https://github.com/Thoughtscript/laravel_php_2026

PHP: Gotcha's

Topics to review.

Types

Ten core Data Types spread across:

  1. Scalars
  2. Compound Types
  3. Special Types
  4. Pseudo Types

Scalars

Any of the Data Types:

  1. bool
  2. int
  3. float
  4. string

Compound Types

  1. array - Associative (Key-Value), Indexed (standard), Multi-Dimenionsal
  2. object - Class Instances (not Alias like in .NET for System.Object)

Special Types

  1. null
  2. Resource - Database

Pseudo Types

  1. Callable - invokable Functions (Anonymous or the String names of Functions)
  2. Iterable - Traversable Interface implementations (incl. Arrays) that are compatible with foreach.

Arrays

  1. $my_arr[] = 4; appends 4 to $my_arr.
  2. 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

  1. constant() allows one to get the Value of a Constant declared through define() or const.
  2. Constants are Globally-scoped.
  3. define() accepts any Expression and is evaluated at Runtime. Can be used anywhere.
  4. const accepts 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

  1. BothMySQLi and PDO will automatically close Connections at the end of the Script.
  2. mysql_fetch_array() still shows up on some Quizzes and Tests but it's long size been deprecated.

Magic Constants

  1. $_REQUEST has all the contents of $_POST, $_GET, and $_COOKIE.
  2. It's widely recommended by the PHP community to use $_POST (Form Body) for large data inputs (rather than $_GET).

Operators

  1. $x .= $y is the same as $x = $x . $y:
    $x = "a";
    $y = "b";
    $x .= $y;
    echo $x; //ab
    
    $x = "a";
    $y = "b";
    $x = $x . $y;
    echo $x; //ab
    
    Not Multiplication - String Concatenation.

Uncaught Exceptions

  1. 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

  1. get_browser() to describe the User's Browser capabilities.

Read and Write Permissions

For use with say fopen():

  1. r+, w, w+, x all give Write Permission.
  2. x - Create and Open. Will throw if File already exists.
  3. w - Open for Writing only. Will create if File doesn't exist.

Cookies

  1. setcookie() must be set before any HTML content (including <html></html> Tags) within the PHP Script.

Interfaces, Abstract Classes, Traits, Classes

  1. traits are akin to Mixins and represent functionalities that can be added to multiple Classes without them sharing Types.
  2. Like Java in many respects:
    • extends, implements keywords.
    • No multiple Inheritance.
  3. Overriding is done by exact Function Name and Signature match. (No Annotation/Decorator.)
  4. Interfaces can only have Constant Fields and Method Signatures.
  5. self (specific Instance and its Class Definition Scope) vs $this (Class Definition).
  6. parent::my_method() - accesses parent Methods from Sub-Classes.
  7. static::my_method()
    • Overrides in the last Descendent Class at Runtime.
    • Adapts my_method() to Descendent Overrides at Runtime.
  8. 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).
  9. :: is the Scope Resolution Operator (akin to C++).
  1. https://www.php.net/manual/en/language.types.array.php
  2. https://www.php.net/manual/en/mysqli.close.php
  3. https://www.php.net/manual/en/language.exceptions.php
  4. https://www.php.net/manual/en/function.get-browser.php
  5. https://www.php.net/manual/en/function.fopen.php
  6. https://www.php.net/manual/en/function.mysql-fetch-array.php
  7. https://www.php.net/manual/en/function.setcookie.php
  8. https://blog.masteringbackend.com/self-static-and-parent-in-php
  9. https://www.php.net/manual/en/language.oop5.static.php