Please support if you like my work by payment through upi: sinhamit@icici or payment by bank
account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB
Type Hinting in Category: PHPlevel2 by amit
🕙 Posted on 2023-06-17 at 10:35:12 Read in Hindi ...
(Data) Types of Parameters
You have learned in previous page that values (from either literals or variables/()
parentheses just after the functionName, before using them inside the custom function block {}
curly braces.
Similarly, what should be the data-type of returned value, after processing all codes inside the custom function block {}
curly braces, can also be handled. Remember that PHP is a loose data-type scripting language. It means that the data-type of a variable name can be changed at any time. Let't see these options to write types of parameters in following examples. Syntax to write type-hinting in a custom function is:
<?php function
functionName( array
$param ) { /* statements inside function block */ } ?><!-- Using single PHP comment just before closing curly brace will cause ERROR! Unlike type-casting, type-hinting are bool int float string array
without parentheses, which can be written just before a parameter name. -->
<?php
function
addition( int
$param1, int
$param2 ) {
var_dump( $param1, $param2 ); /* Outputs first two values of the result, as shown below: */
return
$param1 + $param2; /* Outputs the third value of the result, as shown below: */
}
$num1 = 2.3; $num2 = 3.4;
var_dump( $num1, $num2 ); /* Outputs float(2.3) float(3.4) − original data-type of arguments */
echo '<br />';
var_dump( addition( $num1, $num2 ) ); /* outputs int(2) int(3) int(5) − when function is called, data-type is changed to int
*/?>
In the above example, you can see that within ()
parentheses, just after the custom function name addition, the reserved keyword int
is placed before each parameter name $param1 and $param2. This is type-juggling or type-coersion of arguments, or type-hinting (expecting data-type) of parameters of the custom function. Whatever data-type of each value is passed when calling/
<?php
function
multiply( float
$param1, float
$param2 ) {
var_dump( $param1, $param2 ); /* Data-type and value of each parameters is shown before the result, when this function is called/invoked each time. */
return
$param1 * $param2; /* Outputs the third value of the result, as shown below: */
}
var_dump( multiply( true, false ) ); // outputs float(1) float(0) float(0)
echo '<br />';
var_dump( multiply( 2, '3' ) ); // outputs float(2) float(3) float(6)?>
In the first of above examples, when floating point numbers are passed as arguments, they are converted into integer data-type. The decimal values after the . (dot or decimal point in numeric value) are truncated and therefore, 2.3 and 3.4 are converted into 2 and 3 respectively. The second example is similar to the first example, and boolean values, true
and false
are converted into float(1) and float(0) respectively. Also, in the second example, 2
(integer) and '3'
(string) are converted into float(2) and float(3) respectively.
However, data-type of returned value is not type-hinted yet, in above two examples, and they are default result of expression mentioned after return
keyword, that is, addition of two integers results in a integer data-type. Similarly, multiplication of two floating point numbers results in a float data-type.
<?php
$cms_user = true; /* You can define some variables at the top of your CMS project files */
function
check_user( bool
$cms_user ) {
if( $cms_user ) { $cms_tutorial = 'visible'; }
return
$cms_tutorial;
}
echo check_user( $cms_user ); /* After getting some permission, i.e. when output is visible, you can show tutorials. */?>
In the above example, you can see a practical example generally used in CMS (Content Management System), where type-hinting for parameter can be of bool
data-type. When data-type passed as an argument, is not type-coerced (i.e., not converted into appropriate parameter), it will throw Fatal ERROR. This may sometimes cause some BUGs. You should carefully and smartly write PHP scripts. A complex CMS is just tricks of writing codes smartly.
<?php
function
my_function( string
$param ) {
var_dump( $param ); // outputs ERROR as shown below:
}
$my_arg = []; // any scalar value (but not NULL
) will not output any ERROR
my_function( $my_arg );?>
Fatal error: Uncaught TypeError: my_function(): Argument #1 ($param) must be of type string, array given, called in C:\xampp\
However, even not using the strict mode (see below), you will get ERROR when type-hinting expects array
data-type, and arguments passed in any function are of scalar data-type (which PHP interpreter cannot type-juggle them as parameters) and vice-versa. For example, when string
data-type is expected as parameter in a function, and an array literal is passed as argument, you will get the ERROR as shown in above example.
However, you can use declare(strict_types=1);
(strict mode) to expect only values (as arguments) of those data-type which are mentioned in the ()
parentheses for respective parameters of a custom function. Strict typing is only defined for scalar type declarations. This declare()
statement/declare()
directive in any other PHP file, but not in index.php, following ERROR will be shown:
Fatal error: strict_types declaration must be the very first statement in the script in C:\xampp\
You may get squigly red line ERROR indication in your VSCode Editor, when you write it in your PHP file. This is because your PROJECT FOLDER may not contain the index.php file. You should create a new file, namely, index.php and write strict mode declaration and above example in that file. This will remove the ERROR as shown above.
When the custom function expects the boolean value as argument, and you pass any other data-type, for example, $cms_user = 1; (integer data-type), following Uncaught TypeError will be shown in strict mode. Therefore, while using declare(strict_types=1);
directive (even at the top of index.php file), you have to take care of all other requisites when calling/
Fatal error: Uncaught TypeError: check_user(): Argument #1 ($cms_user) must be of type bool, int given, called in C:\xampp\
While passing integer values, when float data-type is expected in strict mode declaration, ERROR will not be shown. However, when numeric string, that is, numbers within a pair of single/double quotes are passed as arguments, and int
or float
type-hinting is expected as parameter, ERROR will be shown in strict mode.
You will know more about type-hinting of parameters and return type of a custom function, in next page.
Leave a Comment:
Amit Sinha March 2nd, 2023 at 9:30 PM
😃 😄 😁 😆 😅 😂 😉 😊 😇 😍 😘 😚 😋 😜 😝 😶 😏 😒 😌 😔 😪 😷 😵 😎 😲 😳 😨 😰 😥 😢 😭 😱 😖 😣 😞 😓 😩 😫 😤
Ribhu March 3rd, 2023 at 9:30 PM
🐵 🐒 🐶 🐩 🐺 🐱 🐯 🐅 🐆 🐴 🐎 🐮 🐂 🐃 🐄 🐷 🐖 🐗 🐽 🐏 🐑 🐐 🐪 🐫 🐘 🐭 🐁 🐀 🐹 🐰 🐇