Summary of PHP Return, Declare & Tickable Statements - Full PHP 8 Tutorial
Summary of "PHP Return, Declare & Tickable Statements - Full PHP 8 Tutorial"
This video tutorial covers three key PHP concepts: the Return Statement, the Declare Statement, and tickable statements. Below is a detailed outline of the main ideas and methodologies presented.
Main Ideas and Concepts
-
Return Statement
- The
return
statement is used within functions to stop execution and return a value to the calling environment. - If used in a Function, it returns the specified value and continues executing the rest of the script.
- If used in the global scope, it stops the entire script execution.
- The return expression is optional; if omitted,
null
is returned by default.
Example:
Function sum($a, $b) { return $a + $b; } $x = sum(5, 10); // returns 15 echo $x; // outputs 15 echo "Hello World"; // continues execution
- The
-
Declare Statement
- The
declare
statement is used to set directives for the execution of the script. - Three main directives are discussed:
- Tick Directive: Allows custom functions to execute on every tick (a low-level event in PHP).
- Encoding Directive: Specifies the script's encoding (rarely used).
- Strict Types Directive: Enforces strict type checking for Function parameters.
Example of Tick Directive:
declare(ticks=1); // triggers a tick after every statement Function onTick() { echo "Tick\n"; } register_tick_function('onTick');
Example of Strict Types Directive:
declare(strict_types=1); Function sum(int $x, int $y) { return $x + $y; } echo sum(5, 10); // works echo sum(5, "10"); // throws a TypeError
The
declare
statement applies to all subsequent code in the same file but does not affect included files unless declared there as well. - The
-
Goto Statement
The tutorial briefly mentions the
goto
statement but does not elaborate on its usage.
Methodology/Instructions
- Using Return Statement:
- Define a Function with a Return Statement.
- Call the Function and assign its return value to a variable.
- Use the variable in subsequent code to demonstrate continued execution.
- Using Declare Statement:
- Use
declare(ticks=n)
to set the number of statements before a tick occurs. - Register a Function to execute on each tick using
register_tick_function()
. - To enforce strict types, use
declare(strict_types=1)
at the beginning of the file.
- Use
- Including Files:
- To include PHP files, use
require
orinclude
statements. - Ensure strict types are declared in included files if type checking is necessary.
- To include PHP files, use
Speakers/Sources Featured
The tutorial appears to be presented by a single speaker who provides explanations and examples throughout the video. No other speakers or sources are mentioned.
Notable Quotes
— 00:00 — « No notable quotes »
Category
Educational