H2S50: Cross.script-function,call, via include


Hu: One of the most important concepts in server.sideprogramming, on that saves many | lines of code, and speeds up loading<WP.MIC-H2S33>. Our intention is to # define a function in an external | include file, include this file, in some | other script, and be able to call this function, by its name, with the proper arguments, with no frills #

H3S1: The defined | function:

<!-- Status: fully passed 10/18/22, late AM -->
<?php
function check_edit_perm($post_author_username) {
	$session_username = 'blind';
	if ($session_username == $post_author_username) {
		return True;
	}
}
// echo check_edit_perm('blind'); 
?>

<WP.MIC-H2S49> Formatting-note<fbno>: a single = is variable assignment, not a comparison operator; to check for equality, as in our if | conditional, we need to use “==”<php.net, a-r>

H3S2: Possible | traps:

<WP-MIC-H2S39> Hu: The first thing # that PHPdoes, when a script is included, is run that file, to the end, including, for example, any echo statements, that may be in, that other file. This is annoying, and an example of an explicit | limitation, but we should understand that this is part of the control | str of PHP, max.red-<dumb!>, an expression of such, actually instructive, in its simpletonicity<fbno>, but in any case, we should keep that file blank of any visible outputs, as well as any other effects, that we do not want to be triggered, whenever a script is run.

H4S1: Functions are conditionally run, inborn:

Hu: Every function is inherently | conditional, and this behavior of include files teaches us this; any echo statement, or E, that is embedded, and indented, symbolizing this, will not occur, inevitably, and will only occur, when that function, if it is<Turing-2>, called. Therefore, any E that you do not want to be automatic, should be condition-gated inside a function, and this is, very much, the purpose of functions. Moreover, this causes us to stress the importance of delineating functions from E, as E can exist, with, or without, functions, with the pre-expressed difference in execution<Turing>. All of this does behave, absent explicit-imitations, in an expected | way in PHP, and other high.qualitylangs.

H3S2: Using a variable | assignment to initially capture the included.function-output:

$output = check_edit_perm('lucy');

Hu: This variable | assignment | statement, combined with our inclusion of include ‘login.functions-inc.php’; earlier, will run $post_author_username ‘lucy’ through the check_edit_perm() function, with an expected output of null, or false. This expected value will be assigned to our variable, $output.

H3S3: Processing the output, by actioning the variable:

Hu: With the value # of the called | function safely | in-hand, and under the reign of the calling | script, we can now just play “simple | chess“<dream!> and run operations on this variable, in the context of the function’s utility #

// insert chess example

See <WP.MIC-H2S47,H3S4.H4S4> for the continuation.

References:

https://www.php.net/manual/en/functions.returning-values.php

https://www.php.net/manual/en/functions.arguments.php

https://www.php.net/manual/en/language.types.boolean.php

https://www.php.net/manual/en/language.operators.comparison.php


Leave a Reply

Your email address will not be published. Required fields are marked *